Skip to content

Instantly share code, notes, and snippets.

@RaHughes
Last active August 26, 2019 17:26
Show Gist options
  • Save RaHughes/144a816ea1e8fffc060a3d947f8b595c to your computer and use it in GitHub Desktop.
Save RaHughes/144a816ea1e8fffc060a3d947f8b595c to your computer and use it in GitHub Desktop.
JQUERY Selectors

The Selector

  1. How do you select HTML elements using element names, ID, or class? The syntax looks like this
$("[Class or Tag here]").[Property Here]("[Altered Value Here]");
  1. What are the different ways to chain selectors? For instance, how would you select an element that has a class of “bordered-content” AND “ad-aside”? I think the syntax would look something like
$("bordered-content, ad-aside");

This would allow you to select multiple HTML elements with one jquery selector

  1. How can you select many elements with different classes and IDs in a single selector statement? For instance, how can you select an element with the ID of “main” and a different element with an ID of “secondary”? I imagine it would be the same as above, just use commas in your JQuery selector to grab multiple elements in one go

  2. How do you select based on element attributes?

$("[attribute=value]");

Will allow you to grab ALL elements that have that attribute and value/

  1. How do you select based on the state of a checkbox or radio button? From what I've found online, there is a JQuery method called .prop(), so the syntax would look a little something like this
$("#x").prop("checked", true);
$("#x").prop("checked", false);

This would allow you to select it based off whether the checked property was true or flase.

  1. What does the jQuery selector return? Be specific. It returns a JQuery Object, or rather, an element wrapped in a JQuery Object, which is not an array, and entirely different from a Javascript Object.

  2. Create a demo (in CodePen) explaining the use of different kinds of selectors. My failed attempt https://codepen.io/rahughes/pen/aboJXBZ

  3. Where do you go to find the official jQuery docs? https://api.jquery.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment