Awesome, Mike! Thanks for the clear explanation. :o)
Cheers,
Chris
Michael Geary wrote:
Chris, it's like this... ;-)
$() returns a jQuery object, which is an array of DOM elements with
jQuery methods that apply either to the entire array or to the first
element in the array, depending on the method.
One of those methods is .each(), which loops through the DOM element
array and calls your function on each one, setting "this" to the
individual DOM element.
So, inside a $().each( ... ) function, "this" refers to a specific DOM
element. You can call DOM methods on that element and access its
attributes and properties using ordinary JavaScript.
You can wrap a DOM element with $(element) to get a jQuery object
containing that single DOM element. You can then call jQuery methods
on that object.
In many cases, you can use either one interchangeably. Inside an
each() function, these would do the same thing:
alert( this.innerHTML );
alert( $(this).html() );
as would these:
alert( this.id );
alert( $(this).attr('id') );
Naturally, the first one of each of these pairs is faster and is
recommended in most cases.
-Mike
Mike, thanks so much for the advice! :o)
I guess I'm still fuzzy on when I can use 'this' as opposed to
'$(this)'. I would love to use this.myAttr, but didn't think I
could. I really, really appreciate you re-writing my code snippet
to show me what you're talking about. I know that the 'this' vs.
'$(this)' discussion was had not too long ago, but I didn't (or
couldn't) pay too much attention at the time. If you'd rehash it
for me, or point me to the old thread, I'd appreciate that too! :o)
Cheers,
Chris
------------------------------------------------------------------------
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/
--
http://www.cjordan.info
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/