Accessing the elements by index returns the element itself. To call
jQuery methods you'd need to do this:
divs = $('div');
div2 = divs[2];
$(div2).addClass('red');

or alternately you could do:

div2 = $(divs[2]);
div2.addClass('red');

you can also use .eq(#):
div2 = divs.eq(2)
div2.addClass('red');



On May 5, 6:42 pm, kali <maya778...@yahoo.com> wrote:
> my understanding (acc. to book "jQuey in Action" and other sources,
> likehttp://jquery.open2space.com/node/10) is that the standard jQuery
> wrapper always returns an array containing all matching elements..
> however, for some very odd reason I can't get an element by referring
> to its index in the array...
>
> for example:
>
>         divs = $('div');
>         div2 = divs[2];              // this line is ignored (or SOMETHING
> weird is happening here..)
> //      div2.addClass('red');   // so get error here that 'div2.addClass'
> is not a function..
>
> have a page with above code (and other code) 
> here,http://www.mayacove.com/dev/jquery/arrays.html
>
> am stumped here, have been dealing with this for days..  don't get
> what's happening here (using jQuery version 1.2.1 (maybe this only
> works in 1.3?))
>
> now this is actually not a huge deal b/c in troubleshooting this have
> found other methods (like filter :eq and other methods.. but still, I
> think it's strange that I can't make it work with array index..)
>
> thank you...

Reply via email to