[jQuery] How to bind a function only once
I have a group of checkboxes, and as long as any one of the boxes is checked I need a function bound to a select list, but anytime none of the boxes are checked I need the function unbound. What I have below binds and unbinds, but if I select lets say 4 checkboxes the function is bound and called 4 times when I change my select list. How do I just bind it once? $('[EMAIL PROTECTED]').click(function(){ if($([EMAIL PROTECTED]'label_checkbox']).is(:checked)) { $('#label').bind('change', function(){ alert('test'); }); } else { $('#label').unbind('change'); } }); -- View this message in context: http://www.nabble.com/How-to-bind-a-function-only-once-tf2466835.html#a6876978 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] How to bind a function only once
I have a group of checkboxes, and as long as any one of the boxes is checked I need a function bound to a select list, but anytime none of the boxes are checked I need the function unbound. What I have below binds and unbinds, but if I select lets say 4 checkboxes the function is bound and called 4 times when I change my select list. How do I just bind it once? $('[EMAIL PROTECTED]').click(function(){ if($([EMAIL PROTECTED]'label_checkbox']).is(:checked)) { $('#label').bind('change', function(){ alert('test'); }); } else { $('#label').unbind('change'); } }); I would bind the event once, then look at the check boxes when the event occurs. $('#label').bind('change', function(){ if($([EMAIL PROTECTED]'label_checkbox']).is(:checked)) { alert('test'); } }); ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/
Re: [jQuery] How to bind a function only once
Hi Brian - Here is how I would change your code: $('[EMAIL PROTECTED]').click(function(){ $('#label').unbind('change'); if ( $([EMAIL PROTECTED]'label_checkbox']:checked).length ) $('#label').bind('change', function(){ alert('test'); }); }); By making it such that the change event is always unbound from the select, when a checkbox changes, you're guaranteed to never have more than one bound at a time. Hope this helps. --John On 10/18/06, Brian Litzinger [EMAIL PROTECTED] wrote: I have a group of checkboxes, and as long as any one of the boxes is checked I need a function bound to a select list, but anytime none of the boxes are checked I need the function unbound. What I have below binds and unbinds, but if I select lets say 4 checkboxes the function is bound and called 4 times when I change my select list. How do I just bind it once? $('[EMAIL PROTECTED]').click(function(){ if($([EMAIL PROTECTED]'label_checkbox']).is(:checked)) { $('#label').bind('change', function(){ alert('test'); }); } else { $('#label').unbind('change'); } }); -- View this message in context: http://www.nabble.com/How-to-bind-a-function-only-once-tf2466835.html#a6876978 Sent from the JQuery mailing list archive at Nabble.com. ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/ -- John Resig http://ejohn.org/ [EMAIL PROTECTED] ___ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/