If I have a list of items:
<div id="list">
<div class="good">first</div>
<div class="good">second</div>
<div>trash</div>
<div class="good">third</div>
</div>
and I set a bind:
$(document).ready(function () {
$("#list").children(".good").click(function () {
var index = getIndex(this);
alert(index);
});
});
How may I determine the index of the clicked item such that first would alert 0, second: 1 and third: 2 when clicked on?
I tried something similar to but it returns -1 every time.:
function getIndex(obj) {
var $objectArray = $("#list").children(".good");
return $.inArray($(obj), $objectArray);
Any Ideas?
Oh, Fiddle is here: http://jsfiddle.net/Sh9Dt/
Slight edit to add another garbage list, I need it in relation to a specific jquery select.
Working JSFiddle thanks to @adeneo: http://jsfiddle.net/Sh9Dt/1/