0

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

Senthil Kumar Bhaskaran
  • 7,371
  • 9
  • 43
  • 56

3 Answers3

1

$('.class:first-child')

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

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
1

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
André Eriksson
  • 4,296
  • 2
  • 19
  • 16
1

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

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175