[jQuery] element attributes
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Message</TITLE>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2802" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006>Craig, the return value from a $() call is not a
DOM node (HTML element) itself. It is a jQuery object which may
contain references to one or more DOM nodes. Your getElementById version works
because it returns the DOM node itself, not a jQuery object.</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006>Since you know you are working with a single DOM node,
you can use get(0) to get to the (single) DOM node that $() refers to,
e.g.</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006> alert( $("#test").get(0).world
);</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006>For consistency, I would suggest setting the property
the same way:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006> $("#test").get(0).world =
"earth";</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006>If you are working with the underlying DOM node, it's
often handy to get a reference to it and use that:</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006> var node = $("#test").get(0); // same as
getElementById("test");</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006> node.world = "earth";</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006> alert( node.world );</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Verdana size=2><SPAN
class=953373816-13032006>-Mike</SPAN></FONT></DIV><FONT face=Verdana
size=2></FONT><FONT face=Verdana size=2></FONT><BR>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 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>Craig
Kohtz<BR><B>Sent:</B> Monday, March 13, 2006 8:29 AM<BR><B>To:</B>
discuss@jquery.com<BR><B>Subject:</B> [jQuery] element
attributes<BR></FONT><BR></DIV>
<DIV></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2>This is probably a simple question, but I'm having problems finding the
answer...</FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000 size=2>If
I have the following code...</FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2> <div id="test">hi
world</test></FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2>and want to assign my own attribute to
it...</FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2> $("#test").set("world","earth")</FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000 size=2>I
can set it fine, but I can't seem to get it after it has been set. I've looked
for a function to do this in jquery, but can't seem to find one. I also
tried...</FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2> alert (<SPAN class=250391916-13032006><FONT
face=Georgia color=#008000
size=2>$("#test").world);</FONT></SPAN></FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006></SPAN></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006>But this doesn't seem to work either.
However, if I do the following it works fine:</SPAN></FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006></SPAN></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006>
alert(document.getElementById("test").world);</SPAN></FONT></SPAN></DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006></SPAN></FONT></SPAN> </DIV>
<DIV><SPAN class=250391916-13032006><FONT face=Georgia color=#008000
size=2><SPAN class=250391916-13032006>Can anyone help
please?</SPAN></FONT></SPAN></DIV>
<DIV><FONT face=Verdana size=2></FONT> </DIV></BLOCKQUOTE></BODY></HTML>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/