khim (4) [Avatar] Offline
#1
Hi,

I was following along the examples in the book and found out that both:

$('img[alt]')[0]
and
$('img[alt]').get(0)

fragment on page 35 do not work when entered into the Wrap Set Lab page being discussed.

The error message is: error:TypeError: wrappedSet.addClass is not a function

Did I make anything wrong? I am a new comer and needs help.

Thanks,

Khim
mraj (104) [Avatar] Offline
#2
Re: fragments on page 35
Hi Khim,

The Wrap Set Lab expects the result of the text you type into the textbox to yield a jQuery wrapped set -- because it then calls addClass() on that wrapped set.

So, $('img[alt]') will work, because the result is a jQuery wrapped set.
But $('img[alt]')[0] will not work, because the result is a DOM element, and that DOM element doesn't have an addClass() method.
bear.bibeault (675) [Avatar] Offline
#3
Re: fragments on page 35
Thanks mraj, you beat me to it.

Yes, the "Wrapped Set" lab is exactly that -- a lab for wrapped sets. Any expression that results in anything else will, indeed, cause an error.

If you want to reduce a wrapped set to a single element, check out the eq() method, which returns a wrapped set (as opposed to get(), which returns a DOM element).
khim (4) [Avatar] Offline
#4
Re: fragments on page 35
Hi mraj,

Thank you for the explanation.

Khim
khim (4) [Avatar] Offline
#5
Re: fragments on page 35
Thank you for your explanation.

Khim
shripad (1) [Avatar] Offline
#6
Re: fragments on page 35
Quote bear.bibeault : "If you want to reduce a wrapped set to a single element, check out the eq() method, which returns a wrapped set (as opposed to get(), which returns a DOM element). "



Thanks for the solution. i tried using the nth-child() method but ended up with the same error as i got with get(). does the nth-child() method return a DOM element as opposed to a wrapped set?? i even tried the other methods viz. first, last etc with no success. i could only get eq() method to return a wrapped set. please explain. thank you again.
mraj (104) [Avatar] Offline
#7
Re: fragments on page 35
Hi shripad,

Yes, filter :nth-child() does not return a jQuery wrapped set. Rather, it returns an array of DOM elements. Have a look at http://docs.jquery.com/Selectors (in particular, note "Returns: Array<Element(s)>").

Filters :first, :last, etc. also return an array of DOM elements -- see the aforementioned hyperlink.

eq(), filter(), slice(), etc. all return a jQuery wrapped set: http://docs.jquery.com/Traversing