[jQuery] Re: How to test if an event is already bound to an object ?
Thanks, very interesting and clarifying. Actually, I suspect that the loop (or selector) didn't find any element because the event handler I was looking for seems to be plug on a parent element. Anyway, I did it by filtering on the class attribute, that was a cleaner option. Thanks to all. X+ On Apr 29, 5:07 pm, Brandon Aaron [EMAIL PROTECTED] wrote: You should take a look at this short blog post:http://www.learningjquery.com/2007/03/selecting-elements-by-propertie... -- Brandon Aaron
[jQuery] Re: How to test if an event is already bound to an object ?
Hi, The this.$events is never existing as far as I understand (based on firebug), no matter if the link has an event or not. I have events associated to the click that are created with the jquery accordion plugin. I suppose they are normal events, and that's not why they aren't seen. Any idea? Xavier On Apr 27, 8:16 pm, Jeffrey Kretz [EMAIL PROTECTED] wrote: This is not a terribly elegant way to do it, but it does work. $('whatever_elements').each(function(i) { var hasclick = false; if (this.$events this.$events['click']) { for (j in this.$events['click']) { hasclick = true; break; } } if (!hasclick) { jQuery(this).click( function(e) { /* do something else */ } ); } }); It tests for the existence of the jQuery click event, and if there are any functions subscribed to it. Note that an unbound click event still exists, but it has no event handlers attached (which is what the for loop checks). JK -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of xavier Sent: Friday, April 27, 2007 10:42 AM To: jQuery (English) Subject: [jQuery] How to test if an event is already bound to an object ? Hello, I want to add a default javascript function to all my internal links that DON'T already have a behaviour. If i do : jQuery([EMAIL PROTECTED]/]).bind(click,function(){ //jQuery(#main).empty().append(Loading...).load (/test); jQuery(#main).load (/xajax/+this.pathname,{},function() {this.style.opacity=1;}).fadeTo(slow,0.1); return false; }); (it replaces all the normal links by an ajax call) Then it applies it also to links that have already a javascript event to it. Is it possible to select only the links that don't have any event bound to the click event ? Thanks in advance, Xavier
[jQuery] Re: How to test if an event is already bound to an object ?
This is not a terribly elegant way to do it, but it does work. $('whatever_elements').each(function(i) { var hasclick = false; if (this.$events this.$events['click']) { for (j in this.$events['click']) { hasclick = true; break; } } if (!hasclick) { jQuery(this).click( function(e) { /* do something else */ } ); } }); It tests for the existence of the jQuery click event, and if there are any functions subscribed to it. Note that an unbound click event still exists, but it has no event handlers attached (which is what the for loop checks). JK -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of xavier Sent: Friday, April 27, 2007 10:42 AM To: jQuery (English) Subject: [jQuery] How to test if an event is already bound to an object ? Hello, I want to add a default javascript function to all my internal links that DON'T already have a behaviour. If i do : jQuery([EMAIL PROTECTED]/]).bind(click,function(){ //jQuery(#main).empty().append(Loading...).load (/test); jQuery(#main).load (/xajax/+this.pathname,{},function() {this.style.opacity=1;}).fadeTo(slow,0.1); return false; }); (it replaces all the normal links by an ajax call) Then it applies it also to links that have already a javascript event to it. Is it possible to select only the links that don't have any event bound to the click event ? Thanks in advance, Xavier
[jQuery] Re: How to test if an event is already bound to an object ?
Well my question was rather looking for a dirty hack anyway... I will use your way and then move to something cleaner, either using a class or a rel attibute to define what can't be altered. Thanks, Xavier On Apr 27, 8:16 pm, Jeffrey Kretz [EMAIL PROTECTED] wrote: This is not a terribly elegant way to do it, but it does work. $('whatever_elements').each(function(i) { var hasclick = false; if (this.$events this.$events['click']) { for (j in this.$events['click']) { hasclick = true; break; } } if (!hasclick) { jQuery(this).click( function(e) { /* do something else */ } ); } }); It tests for the existence of the jQuery click event, and if there are any functions subscribed to it. Note that an unbound click event still exists, but it has no event handlers attached (which is what the for loop checks). JK -Original Message- From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf Of xavier Sent: Friday, April 27, 2007 10:42 AM To: jQuery (English) Subject: [jQuery] How to test if an event is already bound to an object ? Hello, I want to add a default javascript function to all my internal links that DON'T already have a behaviour. If i do : jQuery([EMAIL PROTECTED]/]).bind(click,function(){ //jQuery(#main).empty().append(Loading...).load (/test); jQuery(#main).load (/xajax/+this.pathname,{},function() {this.style.opacity=1;}).fadeTo(slow,0.1); return false; }); (it replaces all the normal links by an ajax call) Then it applies it also to links that have already a javascript event to it. Is it possible to select only the links that don't have any event bound to the click event ? Thanks in advance, Xavier