JQuery find parent child div label

When I click expandable-icons-cancel , I want to save the child-label of the group-form . Here is the structure:

<div class="form-group ">
 <label class="" style="padding-top:10px;"> Long Title</label>
 <span class="value ">
  <div class="expandable">
   <div class="expandable-icons">
    <img class="expandable-icons-cancel" style="display:none;" src="test/cancel.png">
   </div>
  </div>
 </span>
</div>

      

And the function:

jQuery('.expandable-icons-cancel').click(function() {
  var parentDiv = jQuery(this).parents('div.expandable'); //-works, but how can i get upper? 
  // How to get 2 steps upper to form-group?
  // How to get get child label text of form-group and save in var?
 }

      

+3


source to share


1 answer


You can use closest()

for example

jQuery(this).closest(".form-group");

      

for the label text you can do ...



jQuery(this).closest(".form-group").find("label").text();

      

If you know the label will always be a direct child .form-group

, you can use children()

instead find()

. If there is a possibility for multiple tags and you only want to get the first one, you can put eq(0)

after receiving the tag.

+7


source







All Articles