Query - Logical Operators API reference for Query logical operator methods filter API Reference
Categories

Query - Logical Operators

Test conditions and check for element existence.

Methods

is

$('selector').is(selector);
$('selector').is(fn);

Tests if any element matches the selector or passes the function test.

Parameters

NameTypeDescription
selectorstringCSS selector to test
fnfunctionTest function

Returns

true if at least one element matches, false otherwise.

Usage

if ($('p').is('.active')) {
console.log('At least one paragraph is active');
}

Example

not

$('selector').not(selector);
$('selector').not(fn);

Removes elements matching the selector or passing the function test.

Parameters

NameTypeDescription
selectorstringCSS selector to exclude
fnfunctionTest function, return true to exclude

Returns

Query object containing remaining elements.

Usage

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

Example

contains

$('selector').contains(target);

Tests if the first matched element contains the given element.

Parameters

NameTypeDescription
targetstring, Element, or QuerySelector, element, or Query to check

Returns

true if contained, false otherwise.

Usage

By Selector
if ($('.container').contains('.highlight')) {
console.log('Container has highlighted elements');
}
By Element
const button = document.querySelector('button');
if ($('.toolbar').contains(button)) {
console.log('Button is inside the toolbar');
}
By Query Object
const $inputs = $('input[required]');
if ($('form').contains($inputs)) {
console.log('Form contains required inputs');
}
Shadow DOM
$$('web-component').contains('.shadow-element');

Example

exists

$('selector').exists();

Checks if there are any elements in the Query object.

Returns

true if at least one element exists, false otherwise.

Usage

if ($('.my-element').exists()) {
console.log('Element found');
}

Example

Previous
Iterators
Next
Position & Intersection