Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery find parent div child label [duplicate]

When i click on expandable-icons-cancel I want to save a child label of form-group div. 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 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?
 }
like image 969
Steve Avatar asked Sep 15 '25 05:09

Steve


1 Answers

You can use closest(), like this

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

for the label text you can do...

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

if you know label will always be a direct child of .form-group, you can use children() in place of find(). If there is a possible for multiple labels and you only want to get the first, you can place eq(0) after getting the label.

like image 118
Dallas Avatar answered Sep 17 '25 17:09

Dallas