Query - Logical Operators API reference for Query logical operator methods filter Guide

Query - Logical Operators

Logical operator methods in Query allow you to perform checks and filtering operations on sets of elements.

is

Checks the current matched set of elements against a selector, element, or Query object and returns true if at least one of these elements matches the given arguments.

Syntax

$('selector').is(selector)

Parameters

NameTypeDescription
selectorString or FunctionA string containing a selector expression to match elements against or a function to test each element

Returns

boolean - true if at least one element matches, false otherwise.

Usage

if ($('#myElement').is('.active')) {
console.log('The element is active');
}

exists

Checks if there are any elements in the current Query object.

Syntax

$('selector').exists()

Returns

boolean - true if the Query object contains at least one element, false otherwise.

Usage

if ($('.my-element').exists()) {
console.log('The element exists in the DOM');
} else {
console.log('The element does not exist in the DOM');
}

Quick Check - exists() is a convenient way to check for the presence of elements without needing to compare the length property.

not

Remove elements from the set of matched elements.

Syntax

$('selector').not(selector)

Parameters

NameTypeDescription
selectorString or FunctionA string containing a selector expression to match elements against or a function to test each element

Returns

A new Query object with elements that do not match the selector.

Usage

$('div').not('.ignore').addClass('highlight');

Inverse Selection - not() is particularly useful for excluding specific elements from a larger set based on various criteria.