-1

Let's say I have a html code like this:

<div class="color">
    <span>red</span
</div>
<div class="color">
    <span>green</span
</div>
<div class="color">
    <span>blue</span
</div>

What I want is to get the second div with the class "color". I know I could loop through them with each() and get it from the loop, but I'm pretty sure there's an easier way to do this.

var id = 1

function GetColor(id) {
    //get the second div with the class "color"
}

How could this be done?

holyredbeard
  • 19,619
  • 32
  • 105
  • 171

1 Answers1

1

Since you said jQuery, here you go,

$('div.color').eq(1); // will get the second one.
lshettyl
  • 8,166
  • 4
  • 25
  • 31