J.Vinicius
J.Vinicius

Reputation: 148

Get a child div from a parent div

my question is how can I get a child div from a parent div of a element? Like this:

<div class="parent">
    <div class="child"></div>
    <button class="getchild"></button>
</div>

How can i get the div "child" from a div "parent" by hitting the button? (or any other action)

obs: get a element by $(".child") is not an option because there are a lot of other similar divs with the same class in the page.

Thank you.

Upvotes: 0

Views: 71

Answers (2)

Jack
Jack

Reputation: 8941

You can use siblings(). This will get elements on the same level as the current element. http://api.jquery.com/siblings/

Here's a fiddle of it working: http://jsfiddle.net/4EayF/. Not sure what your deal is.

$('.getchild').on('click', function(){
   var child = $(this).siblings('.child');
});

Upvotes: 4

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

From the button's click handler:

$(this).parents('.parent').find('.child')

Upvotes: 1

Related Questions