Question

How to skip one label having same class name of other in jquery

<label for="myCb1">test1</label>
<label for="myCb1">test</label>
<input type="checkbox" id="myCb1" value="1" />

when i try to invoke the label of myCb1 both are display so please help how to skip one label using jquery

Was it helpful?

Solution

You want to use the :eq pseudo-selector as such:

$("label[for='myCb1']:eq(1)")

:eq allows you to specify the index of the found elements that you want to return. The index is zero-based (which means that the first element will be index 0).

jQuery Docs: :eq pseudo-selector

OTHER TIPS

$('.class:first-child')

This is if they are right next to each other like you just described.

Either give them an id to uniquely identify them, or you can use a selector such as:

$("label[for=myCb1]:eq(0)") // Only selects the first label
$("label[for=myCb1]:eq(1)") // Only selects the second label
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top