Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery get current number of child

Tags:

jquery

tags

<div id="sample">
  <h3>headline 1</h3>
  <h3>headline 2</h3>
  <h3>headline 3</h3>
</div>

How is it now possible to determine the clicked h3 child element and process this with jquery?

var id = $("h3").index();

this is'nt it?

like image 298
Der Admin81 Avatar asked Nov 28 '25 05:11

Der Admin81


2 Answers

Inside click handler, you have to refer to $(this) to refer to the clicked element. Try like

$("h3").on("click",function(){


  alert($(this).index());

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="sample">
  <h3>headline 1</h3>
  <h3>headline 2</h3>
  <h3>headline 3</h3>
</div>
like image 176
Mithun Satheesh Avatar answered Nov 29 '25 21:11

Mithun Satheesh


Pass this inside index function

$("h3").click(function(){
 var id = $("h3").index(this);
})
like image 40
Girish Avatar answered Nov 29 '25 21:11

Girish