[jQuery] Adding class to parent?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2912" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>You've gotten the answer already, but to help for next
time... This confusion between a jQuery object and a DOM element will come up
again and again.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>When some jQuery method doesn't seem to work as you expect,
use your favorite debugger to set a breakpoint on the method call and take a
look around. Examine the variables, step into the code.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>In your code below, you would set a breakpoint on the
line:</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006>$(this.parent()).addClass("selected");</SPAN></FONT></DIV></FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>When you get there, you would look at "this" and discover
that it was a DOM element, not a jQuery object. Or you could try steping into
the parent() call, to discover that parent() is not being called, because "this"
does not have a parent property.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>If you're not in the habit of using a debugger, now is the
time to start. :-)</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>You can even use "alert(this); alert(this.parent);" if you
want to do it that way.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=937511316-10072006><FONT face=Verdana
color=#0000ff size=2>-Mike</FONT></SPAN></DIV><BR>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> discuss-bounces@jquery.com
[mailto:discuss-bounces@jquery.com] <B>On Behalf Of </B>Luc
Pestille<BR><B>Sent:</B> Monday, July 10, 2006 8:54 AM<BR><B>To:</B> jQuery
Discussion.<BR><B>Subject:</B> [jQuery] Adding class to
parent?<BR></FONT><BR></DIV>
<DIV></DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 border=0><!-- Letter Heading -->
<TBODY>
<TR>
<TD><!-- Left margin --></TD>
<TD>
<DIV><FONT face="Trebuchet MS" size=2><SPAN class=234235115-10072006>Hi
all,</SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006>Given a list of links within an html list, how
do I add a class to the <li> that an <a> sits in when
clicked?</SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2></FONT> </DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006><ul id="Links"></SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006><li><a
href="#">blah</a></li></SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006></ul></SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2></FONT> </DIV>
<DIV><FONT face="Trebuchet MS" size=2>$("#<SPAN
class=234235115-10072006>Links</SPAN>
a").click(<BR>function()<BR>{</FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006>$(this.parent()).addClass("selected");</SPAN></FONT></DIV><FONT
face="Trebuchet MS" size=2>}<BR>);</FONT>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006></SPAN></FONT> </DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006>Doesn't seem to work as I expect. Am I using
.parent() incorrectly?</SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2><SPAN
class=234235115-10072006>Thanks,</SPAN></FONT></DIV>
<DIV><FONT face="Trebuchet MS" size=2></FONT> </DIV>
<DIV> </DIV>
<DIV align=left>
<TABLE align=left>
<TBODY>
<TR>
<TD vAlign=top><IMG height=33
src="http://www.in2.co.uk/images/in2-logo-small.gif" width=30></TD>
<TD width=7></TD>
<TD vAlign=top><FONT face="Trebuchet MS" size=2>
<P><B>Luc Pestille</B><BR>Web Designer </P>
<P>e: luc.pestille@in2.co.uk<BR>t: +44 (0)1628 899 700<BR>f: +44
(0)1628 899 701 </P></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV> </DIV></TD>
<TD><!-- Right margin --></TD></TR><!-- Disclaimer -->
<TR>
<TD colSpan=3><BR>
<DIV style="FONT-SIZE: 7pt; COLOR: gray; FONT-FAMILY: verdana"
align=justify>In2<BR>Thames House<BR>Mere Park<BR>Dedmere
Road<BR>Marlow<BR>Bucks<BR>SL7 1PB<BR><BR>Tel 01628 899700<BR>Fax 01628
899701<BR>e: <A href="mailto:sales@in2.co.uk">sales@in2.co.uk</A><BR>i:
<A href="http://www.in2.co.uk/">www.in2.co.uk</A><BR><BR><FONT
color=silver>This message (and any associated files) is intended only
for the use of discuss@jquery.com and may contain information that is
confidential, subject to copyright or constitutes a trade secret. If you
are not discuss@jquery.com you are hereby notified that any
dissemination, copying or distribution of this message, or files
associated with this message, is strictly prohibited. If you have
received this message in error, please notify us immediately by replying
to the message and deleting it from your computer. Messages sent to and
from us may be monitored. Any views or opinions presented are solely
those of the author Luc.Pestille@in2.co.uk and do not necessarily
represent those of the company.
<BR><BR></FONT></DIV></TD></TR></TBODY></TABLE></DIV></BLOCKQUOTE></BODY></HTML>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/