jQuery May 7, 2014
Agenda
- Introduction to jQuery and its API Documentation
- Traffic Light with jQuery
- Color Switcher with jQuery
- FAQ Page
Starter Files
Traffic Light with jQuery (on Codepen)
We’ll look at this again to see a bit about how jQuery works.
Color Scheme Switcher with jQuery (download)
Using the jQuery library, we’ll redo Color Scheme Switcher yet again.
Further Reading
Please take some time to start looking at the jQuery documentation. Oscar Otero also has a really nice table of contents for the docs at his jQuery Cheatsheet.
jQuery allows us to manipulate the page using a much simpler syntax than what we’ve been using so far.
For example, we’ve been writing something like:
document.getElementById('thingy').onclick = doSomething;
function doSomething() {
// make something happen here
}
In jQuery, this might look more like:
$('#thingy').click(doSomething);
function doSomething() {
// make something happen here
}
We will certainly be discussing this in more detail, but in general jQuery lets us grab some element from the page ($('#thingy')
), and do something with it ($('#thingy').click(doSomething);
). In this case, we grabbed an element with the id thingy
and used .click()
to make a function run when the user clicks on #thingy
.
For now, as you start to get used to this idea, I’d like you to look through the documentation above, and have a look at some of these things we can do to an element (or elements) using jQuery:
.toggleClass()
– turn a given class off or on.hasClass()
– check if the element has a given class.hide()
– visually hide the element.show()
– visually show the element.slideUp()
– animate to hide the element.slideDown()
– animate to show the element.siblings()
– find the element’s siblings.removeClass()
– remove a given class from the element.addClass()
– add a given class to the element