jQuery - Notes
08 Aug 2013Using CDN and providing Fallback
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="jquery.js"></script>')</script>
jQuery Hello World
jQuery Selector
Selecting Nodes(Tag) by TagName
Selecting Nodes by ID
$('#myID')
Selecting Elements by Class Name
- Selecting By Attribute Value
- Selecting Input Nodes
- Using Contains in Selector
- Selecting Even or Odd Rows
- Selecting First Child
Example
jQuery each Function
Accessing / Modifying Attributes
Accessing Attributes
Modiying Multiple Attributes
Adding and Removing Nodes
- .append() - Bottom of the Children
- .prepend() - Top of the Children
- .remove() - Remove a Node
$('#outputdiv').append('Added Tag');
Wrapping Elements
- .wrap() - Wraps a selector
Modifying Style
Modifying Classes
- .addClass()
- .removeClass()
- .toggleClass()
Example
<div id="home"></div>
<input id="txt_name" onkeyup="dynatext()" type="text" />
###Handling Events
jQuery simplifies hanlding cross-browser event attachments.
.click(handler(eventObject));
jQuery performs following event handling
- click()
- blur()
- focus()
- dbclick()
- mosuedown()
- mouseup()
- mouseover()
- mouseenter()
- keydown()
$('#id').click(function(){
alert('I am Clicked');
$('#anotherid').click(); //Click Another Item
});
$('#id').change(function() {
alert($(this).val());
});
$('#myid').mouseup(function(e) {
alert(e.pageX);
alert(e.pageY);
});
###Binding to Events
Using on()
$('#mydiv').on('click',function() { //Code Block });
Using off()
$('#mydiv').off('click');
###Binding Multiple Events
$('mydiv').on('mouseenter mouseleave mouseup', function(e) {
if (e.type == 'mouseup') {
alert('I am over');
}
});
$('#mydiv').off('mouseenter');
live(), delegate() and on() allow new DOM elements to automatically be “aatched” to an event handler
$(#$divs').delegate('div','click', somefunction);
The on() function can be used in place of live() and delegate() [Bubbleup Events]
Add future children element to be wired to event handler
This delegated-events approach attaches an event handler to only one element, tbody and the event only needs to bubble up one level.
$('#MyTable tbody").on("click","tr", function(event) {
alert($(this).text());
});
Versus
$('#Mytable tbody tr").on('click', function(event) {
});
###Handling Hover / Toggle Events
$(selector).hover(function() {
//Triggers on HoverOn and HoverOut
});
$(selector).hover(handlerIn, handlerOut);
$(selector).toggle(handlerIn, handlerOut);
#Working with Ajax Features
jQuery AJAX Functions - jQuery allows Ajax requests to be made from a page
- Allows parts of page to be updated
- Cross_browser Support
- Simple API
- GET and POST supported
- Load JSON, XML, HTML or even scripts
jQuery Ajax Functions
Loading HTML Content from the Server
$(selector).load() - Loads HTML data from the server
Syntax :
$(selector).load(url,data,callback)
Example
$(document).ready(function(){
$('#MyDiv').load('HelpDetails.html #MainTOC');
});
$.get() and $.post() - Get raw data from the server
$.getJSON(): Get/POST and return JSON
$.ajax(): Provides core functionality
Making GET Requests
$.get(_URL,callback_);
Making POST Requests
$.post(_URL,data,callback_);