[jQuery] Fw: bind two events to same function
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16414" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Also, on a second note...</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Isn't this exactly the required method of attaching
functions to events if you want to specifically unbind them later on? I was on
the assumption that in order to unbind certain functions from events you need to
pass in the exact same function pointer as a parameter that you used when
binding.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Like</FONT></DIV>
<DIV><FONT face=Arial size=2>$('#clickelem').bind('click',myAlert);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>and then unbinding elsewhere:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2>$('#clickelem').unbind('click',myAlert);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Works perfectly. If you use, however:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>$('#clickelem').bind('click',function(){
alert('foo!'); });</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>and then</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>$('#clickelem').unbind('click',<FONT face=Arial
size=2>function(){ alert('foo!'); }</FONT>);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>It does not work, since those are actually 2
different functions created on the fly (although identical).</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>Plus using the first method is often much cleaner
for the code, too.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>-- </FONT></DIV>
<DIV><FONT face=Arial size=2>Suni</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT><FONT face=Arial size=2></FONT> </DIV>
<DIV style="FONT: 10pt arial">----- Original Message -----
<DIV style="BACKGROUND: #e4e4e4; font-color: black"><B>From:</B> <A
title=juha.suni@sparecom.fi href="mailto:juha.suni@sparecom.fi">Juha Suni</A>
</DIV>
<DIV><B>To:</B> <A title=discuss@jquery.com
href="mailto:discuss@jquery.com">jQuery Discussion</A> </DIV>
<DIV><B>Sent:</B> Wednesday, March 28, 2007 11:49 AM</DIV>
<DIV><B>Subject:</B> Re: [jQuery] bind two events to same function</DIV></DIV>
<DIV><FONT face=Arial size=2></FONT><FONT face=Arial size=2></FONT><BR></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial
size=2>$('#hoverelem').hover(myFunction,myFunction);</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></BODY></HTML>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/