Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

next 3 from this

 <tr>
   <td><div class="click">a</div></td>
 </tr>
  <tr>
   <td><div class="click">b</div></td>
 </tr>
  <tr>
   <td><div class="click">c</div></td>
 </tr>
  <tr>
   <td><div class="click">d</div></td>
 </tr>
  <tr>
   <td><div class="click">e</div></td>
 </tr>
  <tr>
   <td><div class="click">f</div></td>
 </tr>
  <tr>
   <td><div class="click">g</div></td>
 </tr>
<tr>
   <td><div class="click">h</div></td>
 </tr>
<tr>
   <td><div class="click">i</div></td>
 </tr>
<tr>
   <td><div class="click">j</div></td>
 </tr>
<tr>
   <td><div class="click">k</div></td>
 </tr>
<tr>
   <td><div class="click">l</div></td>
 </tr><tr>
   <td><div class="click">m</div></td>
 </tr>
<tr>
   <td><div class="click">n</div></td>
 </tr>
<tr>
   <td><div class="click">o</div></td>
 </tr>  
</table> 

$('.click').click(function(){
    $(this).addClass('red');
})

LIVE: http://jsbin.com/ibofis/1/edit

this working ok, but i would like add for this addClass for 3 next elements.

For example if i click on c then class .red should add for: c, d, e and f.

How can i make it?

like image 728
Berg Swoots Avatar asked Nov 08 '12 14:11

Berg Swoots


People also ask

What time is the first bus 3?

3 bus timetable overview: Normally starts operating at 05:15 and ends at 23:22. Normal operating days: everyday.

Where does the number 3 bus go in Coventry?

3 (National Express Coventry) The first stop of the 3 bus route is Arena Shopping Park and the last stop is Warwickshire Shopping Park. 3 (Binley) is operational during everyday. Additional information: 3 has 55 stops and the total trip duration for this route is approximately 58 minutes.

Is AC7 bus running today?

AC7 (ADVANCE COACH) (Cross Border Service) The first stop of the AC7 (ADVANCE COACH) bus route is Yishun Temp Bus Int and the last stop is Ciq Johor Bahru. AC7 (ADVANCE COACH) (Ac7 Yishun Temp Int - Jb Ciq) is operational during everyday.

What does SBS Transit stand for?

Singapore Bus Services was renamed SBS Transit Ltd on 1 November 2001. The new name reflects our multi-modal status as we move from being just a bus operator to the provision of both bus and train services. We have retained "SBS" in the new company name to remember our roots and preserve our long heritage.


3 Answers

Try this:

$('.click').click(function(){
   var $this = $(this).closest('tr');
   $this.nextUntil().filter(':lt(3)').add($this).find('div').addClass('red');
});

http://jsbin.com/ibofis/2/edit

like image 185
undefined Avatar answered Sep 29 '22 01:09

undefined


Add this to the bottom, after the $(this).addClass('red'); line:

$(this).closest('tr').nextAll(':lt(3)').find('div').addClass('red');
like image 42
irrelephant Avatar answered Sep 29 '22 00:09

irrelephant


$('.click').click(function(){
    $(this).addClass('red');
  var counter = 0, clickedEl = this;
  $('.click').each(function(ind, el){
    if(counter-- > 0){
      $(el).addClass('red');
    }
    if(el == clickedEl){
      counter = 3;
    }
  });
});
like image 44
pckill Avatar answered Sep 29 '22 00:09

pckill