gacon
gacon

Reputation: 2135

How do I select the parent node from a child node?

I'm wrote some jquery code to get the click event on the div inside another div like this.

<div class="parent">
  <div class="values_to_get">
    <div class="catch_click_event_here">
    </div>
  </div>
</div>

So now can someone help me please, I need to get values in the div with class="values_to_get" when I click on the div with class="catch_click_event_here". I have a list of divs with class="parent"

Upvotes: 1

Views: 742

Answers (2)

bendewey
bendewey

Reputation: 40245

If you using jquery 1.3 or greater you can use the closest method

$('.catch_click_event_here').click(function() {
    $(this).closest('.values_to_get').val();
});

Upvotes: 1

Gerardo
Gerardo

Reputation: 1948

$('div.catch_click').click(function (){
    variable = $(this).parent().attr('value');  //this is div.catch_click
});

In that way, you get the content of the 'value' attribute in the "values to get" div. Don't know what values you need, but just replace 'value' with the attribute you are looking for.

Upvotes: 4

Related Questions