.slice()
S2C Home « Filtering « .slice()
Filter subset of elements.
Description
The .slice()
method is used to reduce the matched set to a subset of the specified indices range.
Syntax
Signature | Description |
---|---|
.slice(start [, end] ) | Reduce the matched set to a subset of the specifed indices range. |
Parameters
Parameter | Description | Type |
---|---|---|
start | An integer indicating the 0-based position at which the elements begin to be selected. If negative, it indicates an offset from the end of the set. | Number |
end | An integer indicating the 0-based position at which the elements stop being selected. If negative, it indicates an offset from the end of the set. If omitted, the range continues until the end of the set. | Number |
Return
A jQuery
object containing a subset of the matched set.
.slice(start [, end] )
ExamplesTop
Reduce the matched set to a subset of the specifed indices range.
In the examples below we select all 'td' elements within the table with the class of 'testtable' and then reduce the matched set to the subsets specified. Remember we start from 0 and stop before the 'end' parameter.
Table Row 1, Table Data 1 | Table Row 1, Table Data 2 |
Table Row 2, Table Data 1 | Table Row 2, Table Data 2 |
Table Row 3, Table Data 1 | Table Row 3, Table Data 2 |
$(function(){
$('#btn3').on('click', function() {
$('.testtable td').slice(2, 6)
.css('backgroundColor', 'orange');
});
$('#btn4').on('click', function() {
$('.testtable td').slice(-5, -2)
.css('backgroundColor', 'olive');
});
});
Related Tutorials
jQuery Intermediate Tutorials - Lesson 1 - Filtering Elements