.is()
JQ Home <<
Filtering <<
.is()
Boolean Filter.
Description
The .is()
method is used to compare the matched set against a specified selector, element, jQuery object or function and return true if any element matches the given arguments.
- The
.is()
method does not create a new jQuery object like the other filtering methods do, but allows testing of a jQuery object without modifying the contents.
Syntax
Signature | Description |
---|---|
.is( selector ) | Check the current matched set of elements against a selector. |
.is( element ) | Check the current matched set of elements against an element. |
.is( jQuery object ) | Check the current matched set of elements against a jQuery object. |
.is( function(index) ) | Pass each element within the current matched set through a function. |
Parameters
Parameter | Description | Type |
---|---|---|
selector | A string containing a CSS or custom jQuery selector to match elements against. | Selector |
element | An element to match elements against. | Element |
jQuery object | A jQuery object to match elements against. | jQuery |
function(index) | A function.
|
Function |
Return
A JavaScript Boolean
object.
.is( selector )
Example
Filtering << Top
Check the current matched set of elements against a selector.
In the example below we select all 'td' elements within the table, iterate over the collection changing the background colour to olive. We break from the loop when we hit the 'td' element with the id of 'tdid1'.
Table Row 1, Table Data 1 | Table Row 1, Table Data 2 |
Table Row 2, Table Data 1 (id of tdid1) | Table Row 2, Table Data 2 |
$(function(){
$('#btn8').on('click', function() {
$('.testtable td').each(function (index, tableElement) {
$(this).css('backgroundColor', 'olive');
if ($(this).is('#tdid1')) {
return false;
}
});
});
});
.is( element )
Example
Filtering << Top
Check the current matched set of elements against an element.
In the example below we select all 'i' elements within the main content, iterate over the collection changing the background colour to orange if the parent is a 'p' element.
$(function(){
$('#btn9').on('click', function() {
$('.content i').each(function (index, tableElement) {
if ($(this).parent().is('p')) {
$(this).css('backgroundColor', 'orange');
}
});
});
});
.is( jQuery object )
Example
Filtering << Top
Check the current matched set of elements against a jQuery object.
In the example below we select all even 'td' elements within the table with a class of 'testtable2' and save them to a jQuery object. We then iterate over all 'td' elements within the table changing the background colour to yellow if the 'td' element matches an element in our saved jQuery object and orange otherwise.
Table Row 1, Table Data 1 | Table Row 1, Table Data 2 |
Table Row 2, Table Data 1 | Table Row 2, Table Data 2 |
$(function(){
$('#btn10').on('click', function() {
var $evenCells = $('.testtable2 td:even');
$('.testtable2 td').each(function (index, tableElement) {
if ($(this).is( $evenCells )) {
$(this).css('backgroundColor', 'yellow');
} else {
$(this).css('backgroundColor', 'orange');
}
});
});
});
.is( function(index) )
Example
Filtering << Top
Pass each element within the current matched set through a function.
- If the function returns
true
, the.is()
method returnstrue
as well.
In the example below when you click on a table cell below within the table with a class of 'testtable3' the background colour will change dependant upon that cell having a 'strong' element within it.
Table Row 1, Table Data 1 | Table Row 1, Table Data 2 |
Table Row 2, Table Data 1 | Table Row 2, Table Data 2 |
$(function(){
$('.testtable3 td').click(function() {
var $td = $(this),
isStrong = $td.is(function() {
return $('strong', this).length === 1;
});
if ( isStrong ) {
$td.css("background-color", "yellow");
} else {
$td.css("background-color", "silver");
}
});
});
Related Tutorials
jQuery Intermediate Tutorials - Lesson 1 - Filtering Elements