morne
morne

Reputation: 4189

Traversing up ( parent ) and then down ( child )

I want to achive something like this

var argument = $(this).parent().parent().parent().prev().child().attr('ctryid');

here is a snip of the section I want to traverse. I want to go from newProvButton to the element with ctryid.

its a mess, im sorry.

enter image description here

Upvotes: 0

Views: 417

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337570

You can use closest() to find the related parent element with the given selector. Try this:

var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid');

You should also note that using non-spec attributes in your HTML will render your page invalid. Use a data-* attribute instead:

<div class="expandListHeaderRow" data-ctryid="your-guid">Foo</div>
var argument = $(this).closest('.expandListContent').prev().find('.expandListHeaderRow').data('ctryid');

Upvotes: 3

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can narrow down the selector using .closest():

$(this).closest('.expandListContent').prev().find('div').attr('ctryid');

or

$(this).closest('.expandListContent').prev().find('.expandListHeaderRow').attr('ctryid');

Upvotes: 1

Related Questions