git clone https://github.com/AIS-BYU/vaf-jquery-2015.git
or download it from https://github.com/AIS-BYU/vaf-jquery-2015
IDE + ( or ) + CODE
#Python 3 python3 -m http.server #Python 2 and below python -m SimpleHTTPServer
#Node.js npm install http-server -g *You may need to sudo on Mac http-server
Presented by
jQuery is a fast and feature-rich JavaScript library. It simplifies things like
DOM traversal and manipulation
event binding and handling
animations and effects
and Ajax calls
through an easy-to-use API that works across almost all browser versions
// Vanilla JavaScript
var btn = document.getElementById('btn'),
msg = document.getElementById('msg');
function setMessage() {
// update the message
msg.innerHTML += ' World';
// remove the event listener
if (btn.removeEventListener) {
btn.removeEventListener('click', setMessage, false);
} else if (btn.detachEvent) {
btn.detachEvent('click', setMessage);
}
}
// check for supported event subscription method
if (btn.addEventListener) {
btn.addEventListener('click', setMessage, false);
} else if (btn.attachEvent) {
btn.attachEvent('click', setMessage);
}
// jQuery
$('#btn').on('click', function() {
$('#msg').append(' World');
});
$( document ).ready(function() {
// Your code here.
});
To ensure that your code runs after the browser finishes loading the document, jQuery has a statement known as the ready event:
Q/A
Why would you want to wait until everything in the document is loaded?
The most basic concept of jQuery is to "select some elements and do something with them."
/** By ID **/
$( '#myId' ); // Note IDs must be unique per page
/** By Class Name **/
$( '.myClass' );
/** By Compound CSS Selector **/
$( '#contents ul.people li' );
/** By Attribute **/
$( 'input[name="first_name"]' );
// Beware, this can be very slow in older browsers
// A little more concise
$( 'elem' ).event(...)
//For binding to 2+ events
//A little more readable
$( 'elem' ).on( 'event', ... )
$('.btn').click(function(){ alert("Click"); });
$('.btn').on('click', function(){ alert("Click"); });
jQuery Core comes with several animations.
jQuery UI offers many more.
$( '.btn' ).on( 'click', function(){
$( this ).fadeOut();
});
.hide() .show() .toggle()
.fadeOut() .fadeIn() .fadeToggle()
.slideUp() .slideDown() .slideToggle()
//Notification solution
$( '.notification' ).on( 'click', function(){
$( this ).fadeOut();
});
//Dropdown solution 1
$( '.dropdown-toggle' ).on('click', function(){
$( this ).siblings( '.dropdown-menu' ).fadeToggle();
});
// Dropdown solution 2
$( '.tab-bar > li' ).on( 'click', function(){
$( this ).children( '.dropdown-menu' ).slideToggle();
});
NOTE: Only use ONE of the solutions for the dropdown menu. Adding them both will cause conflicts.
Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page.
Different browsers implement the Ajax API differently. jQuery provides Ajax support that abstracts away painful browser differences.
$.ajax({
url: "test.html",
statusCode: {
404: function() {
alert( "page not found" );
}
}
}).done(function( html ) {
$( this ).addClass( "done" );
$( this ).append( html );
});
Example
Shorthand .ajax() call
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
$.getJSON( 'ajax/test.json', function( data ) {
$( 'body' ).append( data );
});
The .each() method makes looping more concise and less error-prone.
Each time the callback runs it is passed the current loop index, beginning from 0. The callback is fired in the context of the current DOM element, so the keyword this refers to the element.
$( '#load-data' ).on( 'click', function(){
$.getJSON( 'data/customer.json', function( result ){
var data = result.data;
$.each( data, function( key, value ){
var tbl_row = '<tr>';
$.each( value, function( k, v ){
tbl_row += '<td>' + v + '</td>';
});
tbl_row += '</tr>';
$( 'table tbody').append( tbl_row );
});
});
});
$( '#load-data' ).on( 'click', function(){
$.getJSON( 'data/customer.json', function( result ){
var tbl = $( 'table tbody' );
$.each( result.data, function(){
var tbl_row = '<tr>';
$.each( $( this ), function(){
tbl_row += '<td>' + this + '</td>';
});
tbl_row += '</tr>';
tbl.append( tbl_row );
});
});
})
2
This is an optimized solution that I did not share in the forum. It uses some simplifications to make the code a little less complex.