Is there a way to only index a specific element with in a parent? For example:
$('h4').click(function(){
alert($('div h4').index('h4'));
});
<div>
<p>do not include in indexing</p>
<p>do not include in indexing</p>
<p>do not include in indexing</p>
<p>do not include in indexing</p>
<h4>Tag I want to include in index</h4>
<h4>Tag I want to include in index</h4>
</div>
What I want from this is either an alert of 0 or 1 corresponding to the h4 tag I have clicked.
Is this possible with jquery?
Thanks for all the answers, what most of what you guys have offered works, but in working practice it doesn't seem to function. I guess I have issues somewhere else in my page.
Thanks Anyway
try this
$('h4').click(function(){
alert($('div h4').index(this));
});
fiddle example : http://jsfiddle.net/L84QV/
According to documentation:
If we use a string as the .index() method's argument, it is interpreted as a jQuery selector string. The first element among the object's matched elements which also matches this selector is located.
So you could do:
$('h4').click(function(){
var idx = $(this).index('h4');
alert(idx);
});
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With