On This Page
Methods
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
| Name | Type | Description |
|---|---|---|
| selector | string | CSS selector to test |
| fn | function | Test 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
| Name | Type | Description |
|---|---|---|
| selector | string | CSS selector to exclude |
| fn | function | Test 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
| Name | Type | Description |
|---|---|---|
| target | string, Element, or Query | Selector, 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');}