tags:

views:

39

answers:

2

Is it possible to stop a jQuery function with a checkbox (on/off)? Any examples out there?

A: 

Something like so:

function something () {
     if ( $("my.checkbox:checked").size() > 0 ) {
          // do something
     }else{
          // do nothing
     }
}

Something along those lines anyway.

Swizec Teller
Awesome, but to call a STOP or termination of a function, what am I using?
Erik
You can't really stop a function from outside during its execution. At least I've never heard of interrupts existing in javascript ...
Swizec Teller
A: 

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:

$('#myForm').submit(function(e) {
    if (!$('#mycheck').is(":checked")) {
        return false;
    } else {
        //do something...or nothing...whatever...
    }
});

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:

$(".someClass").tipTip({maxWidth: "auto", edgeOffset: 10, enter: myFunction});

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:

opts.enter.call(this);

To this:

if (!opts.enter.call(this)) {
    return false;
}

Then create your myFunction that does this:

function myFunction() {
    return $('#mycheck').is(":checked");
}

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.

treeface
Your logic works, but you haven't answered the crux of my question. Can a function be haulted/stopped. The function I'm referring to is a jquery tip popups, which I want to disable with a checkbox.
Erik
The "crux" of your question was never explicitly stated, so I don't see how we could expect to know what you meant. If you want an answer to this, we need to know what the rest of your code looks like. What is this plugin doing? When is it being called? I have essentially zero information to go on.
treeface
Very Sorry.. The jquery plugin is: http://code.drewwilson.com/entry/tiptip-jquery-plugin
Erik
Im trying to script a checkbox to hide/show the tips? Maybe I should disable the hover? I'm trying this for days!!! Going nuts. My website is http://www.ubspack.com (Fast Quote Link on top).
Erik
Thank you treeface for any help
Erik
No worries. Alright, so you have this tip thing running on anything with a class of whatever you set it to, but you want to halt execution of this if a specific checkbox is not checked? Have you played around with the options in the tiptip enter event? Look at the options under the link you provided. For that option, provide a function that returns $('#mycheck').is(":checked"). If this guy has written his plugin properly, that should cancel termination of the tip. However, now that I'm looking through his source, I notice that he doesn't appear to do this. I'll edit my post with some tips..
treeface
Thank you treeface! Any help would be appreciated. I thought it would be easy just calling his function with unbide... no bueno! Thank you.
Erik
@Erik ...there you go...that should help you sort this out.
treeface
You're awesome! I'll buy you six-pack!
Erik
It worked!!!! It works great!!!! Thank you!!!!
Erik
@Erik :-D glad to help
treeface