jquery – How do I select the parent node from a child node? – Education Career Blog

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"

,

$('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.

,

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();
});

Leave a Comment