- $(el).parent('div'); // didn't work because .parent()<http://docs.jquery.com/Traversing/parent>(only matches the direct parent, not any ancestors) is not the same as .parents() <http://docs.jquery.com/Traversing/parents> (matches all ancestors) - $(el).parents('div'); // returning all matching ancestors is the point indeed - $(el).parents('div').get(0).addClass('myClass'); // should work, may be a testing error (just tested it, worked for me)
Anyway, continue to try things and you'll be efficient with jQuery, don't worry. Coming from another framework and re-learning the lingo is just a phase, it's frustrating but it's worth it. And jQuery may be an acquired taste but it's great once you're used to it. Michel Belleville 2009/11/10 waseem sabjee <waseemsab...@gmail.com> > > > var parent_element = $(".child).parents('div'); > > On Tue, Nov 10, 2009 at 12:16 PM, Savageman <savagema...@gmail.com> wrote: > >> Hello, >> >> I'm using jQuery but am not really used to it (Mootools' user >> here...). >> >> I was trying to add a class to the first parent "div" of an element. >> Here is how I proceeded in the first place : >> - $(el).parent('div'); // Doesn't return anything: why? >> >> My second attempt was better (intermediate steps for better >> comprehension): >> - $(el).parents('div'); // Returns all parent div, cool but I only >> need the first: >> - ($el).parents('div').get(0); // Returns the div I want: yeah again! >> - $(el).parents('div').get(0).addClass('myClass'); // Doesn't work: >> why? >> >> Finally my last attempt (I asked for help) was the good one: >> - $(el).parents('div:first').addClass('myClass'); // Works fine >> >> I was expecting all of the 3 methods to work, but that's not really >> what's happening: kind of frustating! Can someone explain me why, so I >> become efficient with jQuery? >> >> Thank you in advance for the provided help. >> Sincerely, >> Savageman. >> > >