On May 6, 2009, at 12:05 AM, kali wrote:
On May 5, 10:05 pm, mkmanning <michaell...@gmail.com> wrote:
Accessing the elements by index returns the element itself. To call
jQuery methods you'd need to do this:
divs = $('div');
div2 = divs[2];

THIS is what I did........

div2 = divs[2]  --> is IGNORED....

It's NOT ignored. It returns a DOM node ( <div id="test3"> )

Please re-read what mkmanning wrote, because he's absolutely correct.


pls run http://www.mayacove.com/dev/jquery/arrays.html on Firefox with
JS console open, you will see it says:
div2.addClass' is not a function... this is b/c the line div2 = divs
[2]  is IGNORED...

No again. It's because you can't attach a jQuery method to a DOM node. You have to attach it to a jQuery object.

Instead of div2.addClass('red') , try $(div2).addClass('red')

Or better yet, try this:

var div2 = $('div:eq(2)');
div2.addClass('red');

Keep in mind, though, that JavaScript is zero-indexed, so you'll be add the class to the third div.

--Karl

Reply via email to