Codeschool Try Jquery
Codeschool Try Jquery
video lessons
challenges
☑ HTML
content
☑ CSS
style
JavaScript
behavior
1
level
What is jQuery?
What you can do with jQuery
“DOM”
<!DOCTYPE html>
<html>
<head>
<title>jQuery Adventures</title>
browser
</head>
<body>
<h1>Where do you want to go?</h1> 0% 50% 100%
DOM
<p>Plan your next adventure.</p>
</body>
</html>
DOCUMENT
html
head
title
JavaScript
jQuery Adven...
body JavaScript gives developers a
h1 language to interact with the DOM.
Where do...
p
Plan your...
JavaScript JavaScript
DOM
DOM
JavaScript DOM
DOM
DOM
DOM
JavaScript DOM
If our JavaScript uses jQuery to interact
with the DOM then it will work on most DOM
modern browsers.
1.1 What is jQuery?
Basic jQuery usage
this is the jQuery function
jQuery(<code>);
JavaScript
jQuery(document);
JavaScript
<!DOCTYPE html>
<html>
<head>
<title>jQuery Adventures</title>
</head>
<body>
jQuery("h1");
jQuery("p");
= $("h1");
$("p");
short & sweet
<h1>Where do you want to go?</h1> syntax
<p>Plan your next adventure.</p>
</body>
</html>
DOCUMENT
html
$("h1") ; head
title
jQuery Adv...
body
h1
Where do...
p
Plan your...
html
$("h1").text(); head
title
"Where do you want to go"
jQuery Adv...
body
h1
Where do...
p
Plan your...
html
“I’m ready!”
jQuery(document).ready(function(){
DOM
<code>
});
“I’m ready!”
Will only run this code once the DOM is “ready”
jQuery(document).ready(function(){
$("h1").text("Where to?");
});
1 download jQuery
<script src="jquery.min.js"></script>
3 start using it
<script src="application.js"></script>
parent child
Selecting multiple elements
HTML document
$(".promo, #france");
CSS-like pseudo classes
$("#destinations li:first"); body
h1
$("#destinations li:last"); Where do...
h2
filter
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Filtering by traversing
$("#destinations li"); body
h1
Where do...
$("#destinations").find("li"); h2
Plan your...
ul
li
selection traversal
Rome
li
Paris
It takes a bit more code, but it’s faster. li
Rio
2.2 Traversing
Filtering by traversing
$("li:first"); body
h1
Where do...
$("li").first(); h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Filtering by traversing
$("li:last"); body
h1
Where do...
$("li").last(); h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking the DOM
HTML document
2.2 Traversing
Walking the DOM
$("li").first(); body
h1
Where do...
h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking the DOM
$("li").first(); body
h1
Where do...
$("li").first().next();
h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking the DOM
$("li").first(); body
h1
Where do...
$("li").first().next();
h2
Plan your...
$("li").first().next().prev(); ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking up the DOM
HTML document
2.2 Traversing
Walking up the DOM
$("li").first(); body
h1
Where do...
h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking up the DOM
$("li").first(); body
h1
Where do...
$("li").first().parent();
h2
Plan your...
ul
li
Rome
li
Paris
li
Rio
2.2 Traversing
Walking down the DOM
HTML document
2.2 Traversing
Walking the DOM up and down
$("#destinations").children("li"); body
...
children(), unlike find(), only selects direct children ul id="destinations"
li
Rome
li
ul
li
li Paris
Rio
2.2 Traversing
level 3
Working with the DOM
3.1 Manipulating the DOM
Appending to the DOM DOM representation of the document
li class="vacation"
2 remove a DOM node
h2
Hawaiian Vac...
button
Get Price
$399.99
Hawaiian Vac...
button
Get Price
.append(<element>) .prepend(<element>)
Price node (not in the DOM yet)
.after(<element>) .before(<element>) p
$399.99
Hawaiian Vac...
button
Get Price
Get Price
p
$399.99
Hawaiian Vac...
button
Get Price
Get Price
p
$399.99
$399.99
Get Price
.appendTo(<element>) .prependTo(<element>)
Price node (not in the DOM yet)
.insertAfter(<element>) .insertBefore(<element>)
p
$399.99
price.appendTo($('.vacation'));
function() {
// executing the function runs the code
// between the braces
}
We create a function with the function keyword
$(document).ready(function() {
// this function runs when the DOM is ready
});
Hawaiian Vacation
Target all buttons button
Watch for any clicks
Get Price
$('button').on('click', function() {
// runs when any button is clicked jQuery Object Methods
}); .on(<event>, <event handler>)
$399.99
li class="vacation"
Every button will be removed
button
p
The price will be appended to
both .vacation elements
li class="vacation"
this.remove();
button
Not a jQuery object, needs to be converted
p
$(this).remove();
button
Get Price
p
$(this).after(price);
$399.99
Adds the <p> node after the <button>
Get Price
Add the price as a sibling after button
p
$399.99
button
If the button is moved, the price will be moved
Get Price
How do we keep the
p
price as a child of <li>?
$399.99
$(this).parent().parent().append(price); li class="vacation"
h2
$(this).closest('.vacation').append(price); button
Get Price
$399.99
$399.99
.data(<name>, <value>)
$(document).ready(function() {
$('button').on('click', function() {
var price = $('<p>From $399.99</p>');
$(this).closest('.vacation').append(price);
$(this).remove();
});
});
Reads from the data-price attribute
$(document).ready(function() {
$('button').on('click', function() {
var amount = $(this).closest('.vacation').data('price');
var price = $('<p>From $'+amount+'</p>');
$(this).closest('.vacation').append(price);
$(this).remove();
});
}); Each vacation can have its own price
$(document).ready(function() {
$('button').on('click', function() {
var amount = $(this).closest('.vacation').data('price');
var price = $('<p>From $'+amount+'</p>');
$(this).closest('.vacation').append(price);
$(this).remove();
});
});
$(document).ready(function() {
$('button').on('click', function() {
var vacation = $(this).closest('.vacation');
var amount = vacation.data('price');
var price = $('<p>From $'+amount+'</p>');
vacation.append(price);
$(this).remove();
}); We’ll only query the DOM
});
once for this element
$(document).ready(function() {
$('button').on('click', function() {
...
});
});
If we add new buttons anywhere, they will trigger this click handler
<div id='filters'>
...
<button class='onsale-filter'>On Sale Now</button>
<button class='expiring-filter'>Expiring</button>
...
</div>
li
$('.vacation.onsale') li class="vacation"
li class="vacation"
$('.vacation').filter('.onsale')
Class Manipulation li
li class="vacation"
.addClass(<class>) .removeClass(<class>)
li class="vacation"
$('.vacation').filter('.onsale').addClass('highlighted');
<li class="confirmation">
... Clicking this button...
<button>FLIGHT DETAILS</button>
<ul class="ticket">...</ul>
</li>
...will show the ticket
application.js
alert('Hello');
alert($('button').length);
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
$(document).ready(function() {
alert($('button').length);
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
});
$(document).ready(function() {
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
$('.confirmation').on('?', 'h3', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
What event should we watch for?
});
Mouse Events Fires when the mouse is first positioned over the element
$(document).ready(function() {
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
$('.confirmation').on('mouseenter', 'h3', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
}); Show the ticket when the mouse
}); is positioned over the h3
$(document).ready(function() {
$('.confirmation').on('click', 'button', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
$('.confirmation').on('mouseenter', 'h3', function() {
$(this).closest('.confirmation').find('.ticket').slideDown();
});
}); This code is duplicated, how can we refactor this?
function showTicket () {
$(this).closest('.confirmation').find('.ticket').slideDown();
}
function showTicket () {
$(this).closest('.confirmation').find('.ticket').slideDown();
}
$(document).ready(function() {
$('.confirmation').on('click', 'button', showTicket);
$('.confirmation').on('mouseenter', 'h3', showTicket);
});
Don’t add () at the end - that would execute the function immediately
$(document).ready(function() {
$('.vacation').on('?', '.quantity', function() {
});
}); Which event should we use?
keyup http://api.jquery.com/category/events/keyboard-events/
http://api.jquery.com/category/events/form-events/
Whenever the quantity is changed, the total will be updated span #total
a .expand
application.js
Show Comments
$(document).ready(function() {
$('.vacation').on('click', '.expand', function() { ul .comments
// Find the comments ul li
// Show the comments ul
}); li
});
$(this).closest('.vacation').find('.comments') ul .comments
li
ul .comments
jQuery Object Methods
li
.fadeIn() .fadeOut() .fadeToggle() li
ul .comments
fadeIn() .comments on first click, li
fadeOut() .comments on next click.
li
a .expand
The click event will “bubble up”
Follows the link! Show Comments
to each parent node
(goes to the top of the page) ul .comments
li
li
}); li
}); li
The browser will still handle the click event but will prevent it
from “bubbling up” to each parent node.
The click event will “bubble up” but the browser won’t handle it
CSS
style
JavaScript
behavior
DOCUMENT
div #vacations
ul
li .vacation
p .price
p .price
$(this).css('background-color', '#252b30')
.css('border-color', '1px solid #967'); jQuery Object Methods
.css(<attr>, <value>)
$(this).css({'background-color': '#252b30',
'border-color': '1px solid #967'}); .css(<attr>)
p .price
$(this).find('.price').css('display', 'block');
jQuery Object Methods
$(this).find('.price').show(); .show()
application.css
Can we move these to a CSS stylesheet?
.highlighted { $(this).addClass('highlighted');
background-color: #563;
border-color: 1px solid #967;
}
.highlighted .price {
display: block;
}
$(this).toggleClass('highlighted');
jQuery Object Methods
Adds the class if $(this) doesn’t have it, removes it if $(this) already has it
.toggleClass()
.addClass(<class>)
.removeClass(<class>)
p .price
$(this).css({'top': '-10px'});
5.2 Animation
Adding Movement DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
$(this).animate({'top': '-10px'}); ul
});
}); li .vacation
5.2 Animation
Our animation can slide up but not slide down
Moving Back Down DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
$(this).animate({'top': '-10px'}); ul
});
}); li .vacation
p .price
How do we set ‘top’ to ‘0px’ if a second click occurs?
if(<vacation has the class highlighted>) {
// animate the vacation up
} else {
// animate the vacation back down
}
5.2 Animation
Moving Back Down DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
if(<vacation has the class highlighted>) { ul
$(this).animate({'top': '-10px'});
} else { li .vacation
$(this).animate({'top': '0px'}); p .price
}
});
}); jQuery Object Methods
.hasClass(<class>)
$(this).hasClass('highlighted');
true
5.2 Animation
Moving Back Down DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
if( $(this).hasClass('highlighted') ) { ul
$(this).animate({'top': '-10px'});
} else { li .vacation
$(this).animate({'top': '0px'}); p .price
}
});
});
5.2 Animation
Could we speed this up a little? Our customers don’t have all day.
Changing the Speed
$(this).animate({'top': '-10px'});
We can optionally pass in the speed as a
second argument to animate()
$(this).animate({'top': '-10px'}, 400);
5.2 Animation
Moving Back Down DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
if($(this).hasClass('highlighted')) { ul
$(this).animate({'top': '-10px'}, 'fast');
} else { li .vacation
$(this).animate({'top': '0px'}, 'fast');
p .price
}
});
});
And with this we now have specific speeds for our animation
5.2 Animation
Our jQuery animation is now using a ‘fast’ speed
The Next Step with CSS Animations DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
if($(this).hasClass('highlighted')) { ul
$(this).animate({'top': '-10px'}, 'fast');
} else { li .vacation
$(this).animate({'top': '0px'}, 'fast');
p .price
}
});
});
5.2 Animation
Animation Duration DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
}); ul
});
li .vacation
application.css
p .price
.vacation {
transition: top 0.2s;
}
.highlighted {
top: -10px;
}
5.2 Animation
Working with Modern Browsers DOM representation
application.js
DOCUMENT
$(document).ready(function() {
$('#vacations').on('click', '.vacation', function() { div #vacations
$(this).toggleClass('highlighted');
}); ul
});
li .vacation
application.css
p .price
.vacation {
-moz-transition: top 0.2s;
-o-transition: top 0.2s; Unlike jQuery, with CSS we have
-webkit-transition: top 0.2s; to account for specific browsers
transition: top 0.2s;
}
.highlighted {
top: -10px;
}
5.2 Animation
CSS Animations are a huge topic, but worth looking into