Is it possible to stop a jQuery function with a checkbox (on/off)? Any examples out there?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try this:
http://jsfiddle.net/nNREP/1/
There’s no magic to this…you’re simply testing in the script if your checkbox is checked. If you would also like to cancel termination of, for example, a link when it’s clicked (the link’s click event) or a form’s submission (the form’s submit event), simply return false for those specific events like this:
Edit: Your tiptip plugin doesn’t appear to have an option to conditionally terminate the script. You can get around this by doing one of two things….adding a few conditional lines in the plugin or automatically disabling the plugin when the checkbox is unchecked (this is probably going to be comparatively CPU intensive). So let’s focus on the first option…
When you set up your tiptip plugin, put in something like this:
That myFunction will be called at the very start of the “active_tiptip”, before anything else happens. Go to line 101 of the plugin’s source code and you can see how it’s being called. I found this by searching the document for the word “enter”…I found two instances…one when the property is originally being established with an empty function on line 35 and one at the beginning of the active_tiptip() function. Now, if you want to be able to use that myFunction to either cancel or not cancel the execution of the script, change line 101 in the tiptip.js from this:
To this:
Then create your myFunction that does this:
And so, myFunction will return true when checked and false when not. If false, it will halt the execution of the active_tiptip() function, thereby making sure your tooltip doens’t pop up.
That’s about as deep as I’m willing to go for this. The rest you can figure out for yourself.