JQuery selectors: capturing a section of children that have a given tag

Let's say I have an arbitrary number of children (for example, "td" s) of a given element (for example, "tr"), and I need to grab a certain amount of these (say 4) at a given position (say 3; td 3 - 6 7 , in this case). What would be the best request for this?

Please note that I could deal with potentially thousands of children, so I would not cut arrays in thousands on a regular basis.

Edit: No need to go through jQuery if there is a more efficient option that goes straight to the DOM ...

+1
source share
1 answer

You can use .slice() for this, for example:

 $("tr td").slice(2, 7) //of if you have the <tr> $(this).children("td").slice(2, 7) 

The above would get from 3rd to 7th <td> , since it is index 0 . Or a jQuery-less version, let's say you have a <tr> DOM element:

 var tds = tr.getElementsByTagName("td"); for(var i = 2; i<7; i++) { //do something } 

You can test both versions here .

+6
source

All Articles