3

I wanna select some sibling elements. So I used jQuery .next() but I don't know how to repeat.

// Repeat 3 times
$('#after').next().addClass('selected');
$('#after').next().next().addClass('selected');
$('#after').next().next().next().addClass('selected');
.selected {
 color: red;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li id="after" data-num="3">3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>10</li>  
</ul>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

How can I make a repeated jQuery next() as specified value by for loop, like this? (of course this is not working.)

$('#after')
  for(var i=0; i<$('#after').data('num'); i+=1){
    .next().addClass('selected')
  }
;
Jonas
  • 121,568
  • 97
  • 310
  • 388
jiae
  • 80
  • 7
  • You might like to update the question with what you are expecting to happen - eg that just `
  • 6
  • ` will be selected (which is how I see the question, but others obviously don't) – freedomn-m Mar 05 '16 at 15:42