javascript - How to get clicked item index in jQuery collection? Not by index() -
i want index of clicked element
var $inputs = $("input"); $inputs.click(function() { console.log($(this).index()) });
it works following structure div>(input+input+input)
but need case
div>input div>input div>input
inputs not siblings , same index each element. simplest way index?
check index within jquery element collection providing element argument , apply index()
method on element collection.
var $inputs = $("input"); $inputs.click(function() { console.log($inputs.index(this)); });
var $inputs = $("input"); $inputs.click(function() { console.log($inputs.index(this)); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <input> <div> <input> </div> </div> <div> <input> </div> <div> <input> </div> <div> <input> </div>
Comments
Post a Comment