.parent() gets you the immediate parent of the element, .parents()
traverses up through all ancestors, so you could use .parents('.user')
and it wouldn't matter how deep you nest the link. You can also
use .closest(), which finds the closest parent that matches the
specified selector:

$(this).closest('.user').attr('id')

Check out the docs for traversing http://docs.jquery.com/Traversing


On May 5, 7:49 pm, jquerysk <sh.koiz...@gmail.com> wrote:
> I want to find a parent for a button.
> I have set up the html like the following.
> <div id='user_123' class='user'>
> <h1>My name goes here</h1>
> <p>some description</p>
> <a href='Javascript:{}' class='select'>
> </div>
>
> The thing is that I need to specify how many steps I have to go up to
> get
> the
> parent id( see the bottom ) and if I add another tag I migh have to
> change
> this source. Is there any way to
> specify using class ( maybe "$(this).parent(".user).attr("id") ?) so
> that I
> don't need
> to specify how many level I have to go up in order to get that id?
>
> $().ready(function(){
> $(".select").click(function(){
>     alert($(this).parent().attr("id"));
>
> });
> });
>
> Thanks in advance.

Reply via email to