jQuery Tutorial Tutorials - jQuery .is() method

jQuery .is() method

The .is() method in jQuery is used to check the currently matched set of elements against a selector, element, or jQuery object, and return true if at least one of them matches a given parameter.

jQuery .is() method Syntax

.is(selector)
.is(function)
.is(selection)
.is(elements)
Parameter Type Description
selector selector a selector expression.
function() Function A function used as a test for every element in the set.
elements Element DOM elements
selection jQuery A jQuery object

.is(selector)

example

because the <input> element' parent is a form element, it would return true.

HTML

<form><input type="checkbox" /></form>

jQuery

$("input[type='checkbox']").parent().is("form")

Result

true

.is(function)

example

check the matched elements against a function
Highlight the items which's content contains "tutorial" substring.

$('li').each(function(){
    if($(this).is(
        function(index,element){
            return ($(this).text().indexOf('tutorial')>=0)?true:false;
        }
    )){
        $(this).css('background','cyan');
    }
});
// We're just using this code to demonstrate the usage of is(), you can write it in a more concise way:
// $('li').each(function(){
//     if($(this).text().indexOf('tutorial')>=0){
//         $(this).css('background','cyan');
//     }
// }

Try now

.is(elements)

example

Checks against an existing collection of DOM elements with class selected

var elems = document.getElementsByClassName( "selected" );
$( "p" ).click(function() {
  var p = $( this );
  if ( p.is( elems ) ) {
    p.css( "background", "blue" );
  } else {
    p.css( "background", "red" );
  }
});

Try now

Date:2019-08-26 13:43:26 From:www.Lautturi.com author:Lautturi