John John - 1 month ago 8
HTML Question

Get parent id in Jquery/Javascript

How can i get Parent Div id (id="parent") when i click into the input text?
Here is my sample html code:

<div id="parent">
<div class="child">
<input class="form-control" />
</div>
</div>

Answer

$("input.form-control").click(function(){
  
  var parentId= $(this).parent().parent().attr('id');
  alert(parentId);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
  <div class="child">
     <input class="form-control" />
  </div>
</div>

Comments