jQuery introduction

A quick introduction to jQuery and how it works within JavaScript while simplifying writing some code.

Go fullscreen: ⌃⌥F

jQuery introduction

jQuery is JavaScript

  • Simplifies the interface to HTML
  • Provides a bunch of already made functionality
  • Not required to do HTML manipulation

Use from a CDN

Include jQuery from a content delivery network —like we do with Google Fonts

cdnjs ➔

It’s all money from here

Everything that is jQuery is inside the $() function

Use the $() function to select HTML then manipulate the HTML element

Based around CSS selectors

jQuery (and JavaScript) use CSS selectors to grab things in HTML

  • ul, h1, div
  • .dino-list, .bones
  • #stego
  • main > p:first-child
  • etc.
HTML
<h1>Dinosaurs!</h1>
<ul>
  <li>Stegosaurus</li>
  <li class="tri">Triceratops</li>
  <li>Ankylosaurus</li>
</ul>
JavaScript
$('h1')                  // Find all the <h1> tags
$('ul')                  // Find all the <ul> tags
$('ul li:last-child')    // Find the last <li> tags inside <ul> tags
$('.tri')                // Find everything with the class of `.tri`
JavaScript
var $h1 = $('h1');                          // Store a reference to the HTML element
$h1.html('Dinosaurs rock!');                // Change the HTML inside the <h1>

$('ul li:last-child').remove()              // Delete and HTML element
$('ul').append('<li>Apatosaurus</li>');     // Add HTML to the end of the element

var $newLi = $('<li>');                     // Make a new HTML tag, notice the `<>`
$newLi.html('Diplodocus');
$('ul').prepend($newLi);                    // Add HTML to the start of an element

$h1.addClass('mega');                       // Add a class to the HTML element
$newLi.hasClass('dino');                    // Checks if a class exists

$('ul li').each(function () {               // Loop over a bunch of HTML elements
  $(this).addClass('kilo');                 // $(this) is the current HTML element
});

api.jqeury.com is your best friend

It has all the documentation for all the different features that are built into jQuery

Start

jQuery introduction

A quick introduction to jQuery and how it works within JavaScript while simplifying writing some code.