New Wave JavaScript
1 in every 2 of the top 10K websites is using jQuery
jQuery is used by 55.7% of all top 10K websites
(JS market share 90.7%)
Welcome to the world of tomorrow!
<!doctype html>
<html lang="en">
<head>
<title>Including jQuery</title>
</head>
<body>
<h1>Including jQuery</h1>
<script src="assets/03/examples/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
alert('loaded');
});
</script>
</body>
</html>
...
<script src="assets/03/examples/jquery-1.9.1.min.js"></script>
<script>
$(function() {
alert('loaded');
});
</script>
...
Everybody's a jerk. You, me, this jerk.
jQuery
function which is mapped to $
document.querySelectorAll
)
.innerHTML
on it, as it's no DOM Node!// Selecting elements by ID
$('#myId');
// Selecting elements by class name
$('div.myClass');
// Selecting elements by attribute
$('input[name="first_name"event.target]');
// Selecting elements by compound CSS selector
$('#contents > ul.people li');
// Selecting elements using a pseudo selector
$('ul li:nth-child(2)');
// Select all elements that are in the progress of an animation at the time the selector is run
$(':animated');
// Selects all button elements and elements of type button.
$(':button');
// Select all elements that have no children (including text nodes).
$(':empty');
// Selects elements which contain at least one element that matches the specified selector.
$('div:has(p)');
// Parent selector
$('td:parent');
document.querySelector(All)
$('li').each(function() {
console.log($(this));
});
this
, referencing the current node (HTMLElement
), inside a loop
$()
again, to get a jQuery ObjectEverybody loves Hypnotoad!
jQuery.prototype
(cfr. working with objects/instances)jQuery.prototype
is mapped to $.fn
for convenience<h1>CSS</h1>
<ul>
<li>Ham</li>
<li>Bread</li>
<li>Banana</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
// Adjust properties one by one
$('li').css('border', '1px solid red');
$('li').css('width', '200px');
// Adjust multiple properties
$('ul li:nth-child(1)').css({
'font-weight': 700,
'background-color': 'lime',
'width': '250px'
});
});
</script>
$(document).ready(function() {
// Adjust properties one by one
$('li').css('border', '1px solid red').css('width', '200px');
// Adjust multiple properties
$('ul li:nth-child(1)').css({
'font-weight': 700,
'background-color': 'lime',
'width': '250px'
});
});
$(document).ready(function() {
// Adjust properties one by one
$('li').css('border', '1px solid red');
$('li').css('width', '200px');
// Adjust multiple properties
$('ul li:nth-child(1)').css({
'font-weight': 700,
'background-color': 'lime',
'width': '250px'
});
alert($('ul li:nth-child(1)').css('width'));
});
<!doctype html>
<html lang="en">
<head>
<title>jQuery</title>
<style>
p { margin: 8px; font-size:16px; }
.selected { color:blue; }
.highlight { background:yellow; }
</style>
</head>
<body>
<h1>Classes</h1>
<ul>
<li class="selected">Ham</li>
<li>Bread</li>
<li class="selected">Banana</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('li:last').addClass('highlight').removeClass('selected');
});
</script>
</body>
</html>
$(document).ready(function() {
$('li:last').toggleClass('highlight');
if ($('li:last').hasClass('selected')) {
alert('last li has the class "selected"');
}
});
innerHTML
) and $.fn.val() (to get/set the value of a form element)
<select>
<option>Single</option>
<option selected="selected">Single2</option>
</select>
<input type="password" id="pass" value="P4SSW0RD" />
<p>(loading...)</p>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#pass').val('NEWP4SSWORD');
$('p').html(
'Form choices : ' + $('select').val() + ' - ' + $('#pass').val()
);
});
</script>
$.fn.val()
works with all types of inputs (and textareas!)
<p>Once there was a <em title="huge, gigantic">large</em> dinosaur...</p>
<p id="output">(loading...)</p>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#output').html('The emphasis is ' + $('em').attr('title'));
});
</script>
data-*
attributes, one should use $.fn.data()
// Creating new elements from an HTML string
var p = $('<p>This is a new paragraph</p>');
var li = $('<li class="new">new list item</li>');
// Creating a new element with an attribute object
var a = $('<a/>', {
html : 'This is a <strong>new</strong> link',
'class' : 'new',
href : 'foo.html'
});
<div class="container">
<h2>Greetings</h2>
<p>Hello</p>
<p>Goodbye</p>
</div>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('p').after('<p>Injected After</p>');
});
</script>
$(document).ready(function() {
$('p').before('<p>Injected Before</p>');
});
<div class="container">
<h2>Greetings</h2>
<p>Hello</p>
<p>Goodbye</p>
</div>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('<p>Injected After</p>').insertAfter('p');
});
</script>
$(document).ready(function() {
$('<p>Injected Before</p>').insertBefore('p');
});
<ul>
<li>Ham</li>
<li>Bread</li>
<li>Banana</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('ul').prepend('<li>Apple</li>').append('<li>Pear</li>');
});
</script>
<ul>
<li>Ham</li>
<li>Bread</li>
<li>Banana</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('li:last').prependTo('ul');
});
</script>
<ul>
<li>Ham</li>
<li>Bread</li>
<li>Banana</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('li:last').clone().appendTo('ul');
});
</script>
<ul>
<li>Ham</li>
<li>Bread</li>
<li>Banana</li>
</ul>
<p>A paragraph</p>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('p').remove();
$('ul').empty().append('<li>Apple</li>');
});
</script>
All right, which one of you sea dogs has the guts and know-how to harpoon a whale?
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
<script>
$(document).ready(function() {
$('ul.level-2').children().css('border', '1px solid red');
});
</script>
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
<script>
$(document).ready(function() {
$('ul.level-2').find('li').css('border', '1px solid red');
});
</script>
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
<script>
$(document).ready(function() {
$('li.item-a').parent().css('border', '1px solid red');
});
</script>
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>
<script>
$(document).ready(function() {
$('li.item-a').parents('ul').css('border', '1px solid red');
});
</script>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li id="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#third-item').siblings().css('border', '1px solid red');
});
</script>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li id="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#third-item').next().css('border', '1px solid red');
$('#third-item').prev().css('border', '1px solid blue');
});
</script>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li id="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#third-item')
.next().css('border', '1px solid red')
.end()
.prev().css('border', '1px solid blue');
});
</script>
0100100001101001
eventObject
is passed into the handling functionthis
from within the function<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('li').css('cursor','pointer').on('mouseover', function(e) {
$(this).css('color', 'blue');
}).on('mouseout', function(e) {
$(this).css('color', 'black');
}).on('click', function(e) {
$(this).css('color', 'red');
});
});
</script>
$(document).ready(function() {
$('li').css('cursor','pointer').hover(
function(e) {
$(this).css('color', 'blue');
},
function(e) {
$(this).css('color', 'black');
}
).click(function(e) {
$(this).css('color', 'red');
});
});
$(document).ready(function() {
$('li').css('cursor','pointer').bind('mouseover', function(e) {
$(this).css('color', 'blue');
}).bind('mouseout', function(e) {
$(this).css('color', 'black');
}).bind('click', function(e) {
$(this).css('color', 'red');
});
});
jQuery.Event#target
holds the element that was clickedjQuery.Event#preventDefault()
to prevent the default actionjQuery.Event#stopPropagation()
to stop bubbling of the event$(document).ready(function() {
// Bind Events
$('li').css('cursor','pointer').on('mouseover', function(e) {
$(this).css('color', 'blue');
}).on('mouseout', function(e) {
$(this).css('color', 'black');
}).on('click', function(e) {
$(this).css('color', 'red');
});
// Append new list item
$('ul').append('<li>new list item, injected after the event binding</li>');
});
$(document).ready(function() {
// Bind Events, using delegate
$('ul').on('mouseover', 'li', function(e) {
$(this).css('color', 'blue');
}).on('mouseout', 'li', function(e) {
$(this).css('color', 'black');
}).on('click', 'li', function(e) {
$(this).css('color', 'red');
}).css('cursor','pointer');
// Append new list item
$('ul').append('<li>new list item, injected after the event binding</li>');
});
$(document).ready(function() {
// Bind Events, using delegate
$('ul').delegate('li', 'mouseover', function(e) {
$(this).css('color', 'blue');
}).delegate('li', 'mouseout', function(e) {
$(this).css('color', 'black');
}).delegate('li', 'click', function(e) {
$(this).css('color', 'red');
}).css('cursor','pointer');
// Append new list item
$('ul').append('<li>new list item, injected after the event binding</li>');
});
$(document).ready(function() {
// Bind events, using live (not recommended!)
$('ul li').live('mouseover', function(e) {
$(this).css('color', 'blue');
}).live('mouseout', function(e) {
$(this).css('color', 'black');
}).live('click', function(e) {
$(this).css('color', 'red');
}).css('cursor','pointer');
// Append new list item
$('ul').append('<li>new list item, injected after the event binding</li>');
});
$(document).ready(function() {
// One delegate to rule them all
$('table').on('click', 'td', function(e) {
$(this).css('background', 'gray');
});
});
$.fn.delegate()
, $.fn.live()
). If you base your code on outdated examples, adjust them accordingly.
Made From 100% Recycled Pixels
<button id="go">Run</button>
<div id="block" style="background-color:#bca; width:100px; border:1px solid green;">Hello!</div>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
$('#go').on('click', function() {
$('#block').animate({
width: '70%',
opacity: 0.4,
marginLeft: '40px',
fontSize: '3em',
borderWidth: '10px'
}, 1500 );
});
});
</script>
$.trim(' lots of extra whitespace ');
$.each([ 'foo', 'bar', 'baz' ], function(idx, val) {
console.log('element ' + idx + 'is ' + val);
});
$.each({ foo : 'bar', baz : 'bim' }, function(k, v) {
console.log(k + ' : ' + v);
});
$.inArray(4, [ 1, 2, 3, 4, 5 ]); // 3
$.extend({}, object1, object2);
<p>(loading...)</p>
<script src="jquery-1.9.1.min.js"></script>
<script>
$(document).ready(function() {
var defaults = {
'color' : 'lime',
'font-size' : '15px',
'font-weight' : '700',
'font-family' : 'verdana'
}
var write = function(what, options) {
var cssSettings = $.extend({}, defaults, options);
$('p').css(cssSettings).html(what);
}
write('This will not be lime', {'color':'blue'});
});
</script>
Welcome to the world of tomorrow!