Deal of the Day

Home » Main » Manning Forums » 2007 » jQuery in Action

Thread: fragments on page 35

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 6 - Pages: 1 - Last Post: Dec 29, 2009 10:59 AM by: mraj Threads: [ Previous | Next ]
khim

Posts: 4
From: Los Angeles
Registered: 10/23/09
fragments on page 35
Posted: Oct 23, 2009 6:12 PM
  Click to reply to this thread Reply

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


Posts: 64
From: NJ
Registered: 7/1/09
Re: fragments on page 35
Posted: Oct 26, 2009 12:16 PM   in response to: khim in response to: khim
  Click to reply to this thread Reply

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


Posts: 487
From: Austin TX
Registered: 8/15/05
Re: fragments on page 35
Posted: Oct 26, 2009 12:23 PM   in response to: mraj in response to: mraj
  Click to reply to this thread Reply

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

Posts: 4
From: Los Angeles
Registered: 10/23/09
Re: fragments on page 35
Posted: Oct 29, 2009 4:59 PM   in response to: mraj in response to: mraj
  Click to reply to this thread Reply

Hi mraj,

Thank you for the explanation.

Khim

khim

Posts: 4
From: Los Angeles
Registered: 10/23/09
Re: fragments on page 35
Posted: Oct 29, 2009 5:00 PM   in response to: bear.bibeault in response to: bear.bibeault
  Click to reply to this thread Reply

Thank you for your explanation.

Khim

shripad

Posts: 1
From: mlore
Registered: 12/29/09
Re: fragments on page 35
Posted: Dec 29, 2009 9:34 AM   in response to: khim in response to: khim
  Click to reply to this thread Reply

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


Posts: 64
From: NJ
Registered: 7/1/09
Re: fragments on page 35
Posted: Dec 29, 2009 10:59 AM   in response to: shripad in response to: shripad
  Click to reply to this thread Reply

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

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff