Skip to content

Instantly share code, notes, and snippets.

@tylucaskelley
Last active October 11, 2015 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylucaskelley/c27b2023b02a4a1a5980 to your computer and use it in GitHub Desktop.
Save tylucaskelley/c27b2023b02a4a1a5980 to your computer and use it in GitHub Desktop.
Udacity jQuery Webcast: Examples of Using "$()"
$(document).ready(function() { // just like before, it's good practice to wrap your code in this callback function
// we can select elements by tag name
var $body = $("body");
// this creates what is called a "jQuery object", which comes with a
// ton of convenience methods for DOM manipulation. Here's one:
$body.css("color", "white");
// we can also look for elements using a combination of tag name, class, and ID
var $title = $("h1.title");
// another method we can use on a jQuery object
// this one lets us get the height of an element, including border, margins, etc.
var titleHeight = $title.outerHeight();
// whoa, nested elements! notice the mixing of using the CSS child selector and spaces
var $paragraph = $("body > div > .foo p.bar");
// yet another helper method: set the text of an element
$paragraph.text("Hi, I'm a paragraph!");
// we can also chain methods together!
$paragraph.text("new text").css("border", "1px solid black");
// not as useful, but we can turn regular objects into
// jQuery objects to take advantage of a few functions
var obj = $({ name: "Ty", occupation: "Software Engineer" });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment