How to select an HTML element using jQuery?

To select an HTML element using jQuery, you can use the following syntax:

$(selector)

Here, selector can be element names, CSS selectors, or jQuery-specific selectors.

Examples:

  1. Selecting an element by element name:
$("p") // Selects all <p> elements
  1. Selecting an element by CSS selector:
$(".myClass") // Selects all elements with class name "myClass" $("#myId") // Selects the element with id "myId"
  1. Selecting multiple elements:
$("p, .myClass") // Selects all <p> elements and elements with class "myClass"

You can perform various operations on the selected element(s) such as modifying their attributes, styles, or content. For example:

$("p").addClass("blue") // Adds the class "blue" to all <p> elements

Remember to include the jQuery library in your HTML file before using the jQuery syntax.