jQuery selectors are used to select DOM elements from an HTML document, so that you can manipulate them using jQuery methods. Here are some examples of how to select DOM elements using jQuery selectors:
1. Selecting by ID:
$('#myElement'); // select the element with ID "myElement"
2. Selecting by class:
$('.myClass'); // select all elements with class "myClass"
3. Selecting by element type:
$('p'); // select all <p> elements
4. Selecting by attribute:
$('a[href="https://www.example.com"]'); // select all <a> elements with href="https://www.example.com"
5. Selecting by multiple conditions:
$('input[type="text"].myClass'); // select all <input> elements with type="text" and class "myClass"
jQuery selectors can also be combined with each other, and with other jQuery methods, to perform more complex manipulations of DOM elements. For example:
$('#myElement').addClass('myClass'); // add class "myClass" to the element with ID "myElement"
$('.myClass').text('Hello, world!'); // set the text of all elements with class "myClass" to "Hello, world!"
$('p:first-child').css('color', 'red'); // set the color of the first <p> element to red
In these examples, we're using jQuery methods such as .addClass(), .text(), and .css() to manipulate the selected elements. By combining selectors with these methods, we can perform powerful and flexible manipulations of the DOM.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
jQuery selectors are used to select DOM elements from an HTML document, so that you can manipulate them using jQuery methods. Here are some examples of how to select DOM elements using jQuery selectors:
1. Selecting by ID:
2. Selecting by class:
3. Selecting by element type:
4. Selecting by attribute:
5. Selecting by multiple conditions:
jQuery selectors can also be combined with each other, and with other jQuery methods, to perform more complex manipulations of DOM elements. For example:
In these examples, we're using jQuery methods such as .addClass(), .text(), and .css() to manipulate the selected elements. By combining selectors with these methods, we can perform powerful and flexible manipulations of the DOM.