SyntaxStudy
Sign Up
jQuery jQuery .is() for Testing
jQuery Intermediate 4 min read

jQuery .is() for Testing

.is() is the testing method of the jQuery filtering family. Unlike .filter(), which returns a new jQuery object, .is() returns a boolean — true if at least one element in the set matches the argument, false otherwise. It does not change the selection.

Common Use Cases

Use .is() in conditional logic: checking if a clicked element is of a certain type, whether a form field is required, or whether a panel is currently visible before triggering an animation.

Arguments

.is() accepts the same argument types as .filter(): a selector string, an element or jQuery object for direct comparison, or a function with the same index/element signature.

  • .is(':visible') — true if element is displayed
  • .is(':checked') — true if checkbox is checked
  • .is('a') — true if element is an anchor tag
  • .is(this) — identity comparison
  • .is(function(i, el){ return ... }) — functional test

Event delegation relies heavily on .is() internally — when jQuery checks whether a bubbled event target matches the delegated selector. You can use it explicitly for the same purpose in custom delegation logic.

Example
// Guard animation: only slide down if currently hidden
$('#toggle-btn').on('click', function () {
    if ($('#panel').is(':hidden')) {
        $('#panel').slideDown(300);
    } else {
        $('#panel').slideUp(300);
    }
});

// Check element type in a delegated handler
$('#toolbar').on('click', '*', function () {
    if ($(this).is('button')) {
        console.log('Button clicked:', $(this).text());
    } else if ($(this).is('a')) {
        console.log('Link clicked:', $(this).attr('href'));
    }
});

// Validate: mark required empty fields
$('form input').each(function () {
    if ($(this).is('[required]') && $(this).val() === '') {
        $(this).addClass('error');
    }
});
Pro Tip

Use .is() in if-statements to check conditions before acting — it returns a boolean and never changes the selection.