4

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?
 }
Kevin
  • 2,752
  • 1
  • 24
  • 46
Steve
  • 415
  • 2
  • 8
  • 24

1 Answers1

11

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.

Smern
  • 18,746
  • 21
  • 72
  • 90