Source: JavaScript and jQuery by Jon Duckett
Simplies common JS tasks with full browser compatibility.
jQuery()
$('li.hot')
(More common shorthand)$('li.hot').addClass('complete');
</body>
jquery-1.11.0.min.js
<script src="js/jquery-1.11.0.js"></script>
<script src="js/example.js"></script>
</body>
Example
$(':header').addClass('headline'); //selects all h1-h6 headings, adds headline class
$('li:lt(3)').hide().fadeIn(1500); //select first 3 list items. Hide them. Fade into view.
$('li').on('click', function(){ //set event listener on each list item. On click, remove it.
$this.remove();
});
$('li').html()
only retrieves html from first li
$('ul').text()
retrieves the full list (because it includes descendents)
$('li').html('Updated')
will apply to every list elementYou can chain methods
$('#thing')
.hide()
.delay(500)
.fadeIn(1400);
Check if page is ready with .ready()
$(document).ready(function(){
// your code
});
$(function(){
// your code
});
.text()
.html()
.replaceWith()
.remove()
You can use and amend content by passing in a function as a param
$('li.hot').html(function(){
return '<em>' + $()this.text() + '</em>';
});
let $newElement = $('<li>')
creates an empty list element assigned to $newElement.before()
.after()
.prepend()
/ .appendTo()
.append()
/ .prependTo()
.attr()
$('li#one').attr('id');
$('li#one').attr('id', 'hot')
.removeAttr()
.addClass()
.removeClass()
Getting
var backgroundColor = $('li').css('background-color');
Setting
$('li').css('background-color', '#272727');
// += to increase/decrease pixels
$('li').css('padding-left', '+=20');
//multiple properties w/ object literal
$('li').css({
'background-color': '#272727',
'font-family': 'Courier'
});
Perform one/more statement on each object in a selection (like a loop)
// run on all list elements
$('li').each(function(){ //anonymous function as param
var ids = this.id; // access id property of each li
$(this).append(' <span class ="order">' + ids + '</span>'); //add new <span> after each li with its id listed
//using $(this) here creates a jQuery object so you can use .append() method
});
Handle all events! As usual, use a selector to create jQuery selection. Use .on() to indicate event to respond to.
$('li').on('click', function(){ //select all list elements, assign click handler, pass in anonymous function
$(this).addClass('complete'); //when clicked, add complete class
});
.on() has two optional properties
.on(events[, selector][, data], function(e));
Delegating events
$(function(){
var listItem, itemStatus, eventType;
$('ul').on(
'click mouseover',
':not(#four)',
{status: 'important'},
function(e) {
listItem = 'Item: ' + e.target.textContent + '<br />';
itemStatus = 'status: ' + e.data.status + '<br />';
eventType = 'Event: ' + e.type;
$('#notes').html(listItem + itemStatus + eventType);
}
);
});
Event handling functions recieve object event w/ methods & properties of the event
$('li').on('click', function(e){ //give the event object a name!
eventType = e.type; //get the type of the event!
});
Pattern to load from a CDN
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery-1.10.2.js"></script>')
</script>
Where to place?
<head>
but this makes the page slow to load</body>
so page rendering unaffectedSource: https://www.codefellows.org/blog/6-reasons-for-pair-programming/
Pair programming is the practice of 2 developers working together. One will “drive”: write the code; and one will “navigate”: provide ideas and feedback.