Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to execute a function before to call another function

I have a button with an onClick calling a function that unfortunately I can’t change. This function performs a login to the application:

<Button onClick={() => signIn()}>

but I need to execute some other code, to check if the user has checked a few checkboxes. Is there a way to call another function before the current one, and prevent the function call if the first one return false?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can get the old onclick value, then replace it with your function that calls the original when it’s done checking.

let oldFunc = button.onclick;
button.onclick = function(event) {
    // code to check if the checkboxes are checked
    if (boxes_checked) {
        if (typeof oldFunc == 'string') {
            eval(oldFunc);
        } else {
            return oldFunc(event);
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading