Using next () x the number of times with jQuery

What is an easy way to repeat x the number of times with next() (each time using the same function)?

I work at Sharepoint and have limited control over HTML; I can find the element by its identifier, track the nearest <td> , hide() it, and then move on to the next one (I don’t want all <td> to be around 7 or 8 in a row).

The code below works, but it is not.

 $("#my-easily-identifiable-id").closest("td").hide(); $("#my-easily-identifiable-id").closest("td").next().hide(); $("#my-easily-identifiable-id").closest("td").next().next().hide(); $("#my-easily-identifiable-id").closest("td").next().next().next().hide(); [ ... etc ... ] 

What is the best way to do this?

thanks

PS: added fiddle (genius)

+6
source share
4 answers

Use .nextAll() + .andSelf() with .slice() .

 $("#my-easily-identifiable-id").closest("td").nextAll().andSelf().slice(0, 7); 
+9
source

I think a simpler solution than those that have been published so far is .nextUntil() :

 //to get next 8 elements var i = $('#my-easily-identifiable-id').index(); $('#my-easily-identifiable-id').closest('td').nextUntil('', ':lt(' + (i+8) + ')'); //to get self and next 3 var i = $('#my-easily-identifiable-id').index(); $('#my-easily-identifiable-id').closest('td').nextUntil('', ':lt(' + (i+3) + ')').andSelf(); 

Captures all the "next" elements until the filter is damaged (in this case, we select the next 8 elements). Confirmed by jsFiddle .

+6
source

I have not tried it, but maybe the following may work (I will test for a moment):

 $("#my-easily-identifiable-id").siblings().slice($(this).index(),($(this).index() + 8)).hide(); 

Tested and verified with JS Fiddle demo .

+1
source

Maybe something like this:

 $("#my-easily-identifiable-id").closest("td").hide(); $("#my-easily-identifiable-id").closest("td").nextAll().hide(); 
0
source

Source: https://habr.com/ru/post/898857/


All Articles