Lesson 2
jQuery Selectors
Target and manipulate HTML elements
The selector is a crucial part of the jQuery statement. Selectors find and manipulate HTML elements. Luckily, these selectors are based off of CSS Selectors. Now that you’ve linked the jQuery library to index.html, you can start choosing some HTML elements to alter. Let’s begin with all of the <p> elements. Select all <p> elements by starting your code with:
$("p")
Great job so far! To recap, a selector is a reference to an HTML element that you put between the quotations. Now that we’ve singled out all paragraph elements, let’s add an action. Finish the statement by adding the .hide()
action to the selector. The full phrase should look like this:
$("p").hide()
#id Selectors
Remember using the id tag in index.html? You can also point jQuery to id selectors just as you would in a CSS document.
$("#intro")
.class selectors
In addition to #id, the .class selector finds a specific class element within the HTML document.
$(".mast")
Tools
Check out this complete list of selectors by W3.
Back to Lesson 1: Syntax
Forward to Lesson 3