phpXperts - 09
phpXperts - 09
What is jQuery
Javascript Library
Fast and concise
Simplifies the interaction between HTML and JavaScript
phpXperts - 09
Why jQuery?
Lightweight : 19KB in size (Minified and Gzipped)
CSS1 - 3 Complaint
Cross Browser
 (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome)
phpXperts - 09
Why jQuery?
Lightweight : 19KB in size (Minified and Gzipped)
CSS1 - 3 Complaint
Cross Browser
 (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome)
Great Community Plugins
Tutorials TestCoverage
Open (free) license Books
phpXperts - 09
A ajax and DOM manipulation example
if (!document.ELEMENT_NODE) { document.ELEMENT_NODE = 1;
document.ATTRIBUTE_NODE = 2; document.TEXT_NODE = 3;
document.CDATA_SECTION_NODE = 4; document.ENTITY_REFERENCE_NODE = 5;
document.ENTITY_NODE = 6; document.PROCESSING_INSTRUCTION_NODE = 7;
document.COMMENT_NODE = 8; document.DOCUMENT_NODE = 9;
document.DOCUMENT_TYPE_NODE = 10; document.DOCUMENT_FRAGMENT_NODE = 11;
document.NOTATION_NODE = 12; } document._importNode = function(node,
allChildren) { switch (node.nodeType) { case document.ELEMENT_NODE: var
newNode = document.createElement(node » .nodeName); /* does the node
have any attributes to add? */ if (node.attributes && node.attributes »
.length > 0) for (var i = 0; il = node.attributes.length; » i < il)
newNode.setAttribute(node.attributes » .nodeName,
node.getAttribute(node.attributes[i++] » .nodeName)); /* are we going
after children too, and does » the node have any? */ if (allChildren &&
node.childNodes && » node.childNodes.length > 0) for (var i = 0; il =
node.childNodes.length; » i < il)
newNode.appendChild(document._importNode » (node.childNodes[i++],
allChildren)); return newNode; break; case document.TEXT_NODE: case
document.CDATA_SECTION_NODE: case document.COMMENT_NODE: return
document.createTextNode(node.nodeValue); break; } };
http://www.alistapart.com/articles/crossbrowserscripting
phpXperts - 09
It’s just a single line in jQuery
$(“#content”).load(“page.html #content”);
phpXperts - 09
Who’s using jQuery?
http://docs.jquery.com/Sites_Using_jQuery
phpXperts - 09
Dominating the world
Google trends comparison of last JS framework 12 months
http://www.google.com/trends?q=jQuery%2C+prototype%2C+yui%2C+dojo%2C+mootools&ctab=0&g
eo=all&date=ytd&sort=0
phpXperts - 09
Let’s Start
Download jQuery
http://docs.jquery.com/Downloading_jQuery
phpXperts - 09
Embed in you page
<html>
<head>
<script src=“path/to/jquery-x.x.js"></script>
</head>
<body> … </body>
</html>
phpXperts - 09
Embed in you page
<html>
<head>
<script src="path/to/jquery-x.x.js"></script>
<script>
$(document).ready(function(){
// Start here
});
</script>
</head>
<body> … </body>
</html>
phpXperts - 09
$(“div”).addClass(“xyz”);
Find Some Elements
Do something with them
{
}jQuery Object
jQuery philosophy
phpXperts - 09
A Basic Example
<body>
<div>
<p>I m a paragraph 1</p>
<p>I m a paragraph 2</p>
</div>
<p>I m another paragraph</p>
</body>
phpXperts - 09
A Basic Example
<body>
<div>
<p>I m a paragraph 1</p>
<p>I m a paragraph 2</p>
</div>
<p>I m another paragraph</p>
</body>
Select all paragraphs.
$(“p”)
phpXperts - 09
A Basic Example
<body>
<div>
<p class=“red”>I m a paragraph -1</p>
<p class=“red”>I m a paragraph -2</p>
</div>
<p class=“red”>I m another paragraph</p>
</body>
Select all paragraphs. Add a class to them.
$(“p”).addClass(“red”);
phpXperts - 09
Selector Basics
Just pass a selector to $()
What is selector?
Use any CSS selector
phpXperts - 09
Selector Basics
Think about your simplest css file.
#header{
margin : 0 auto;
}
div{
margin : 0px;
padding : 0px
}
ul.menu li{
…..
}
phpXperts - 09
Selector Basics
The red colored items are selectors
#header{
margin : 0 auto;
}
div{
margin : 0px;
padding : 0px
}
ul.menu li{
…..
}
phpXperts - 09
Selector Basics
Selecting By Id
 $(“#header”)
Selecting using selectors
phpXperts - 09
Selector Basics
Selecting By Id
 $(“#header”)
Selecting By Class
 $(“.updated”)
Selecting using selectors
phpXperts - 09
Selector Basics
Selecting By Id
 $(“#header”)
Selecting By Class
 $(“.updated”)
Selecting by tag name
 $(“table”)
Selecting using selectors
phpXperts - 09
Selector Basics
Selecting By Id
 $(“#header”)
Selecting By Class
 $(“.updated”)
Selecting by tag name
 $(“table”)
Combine them
 $(“table.user-list”)
 $(“#footer ul.menu li”)
Selecting using selectors
phpXperts - 09
Basic Selector Example
This is my page
<body>
<div id=“header”>
<span id=“logo”>Logo here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
phpXperts - 09
Basic Selector Example
$(“#header”)
<body>
<div id=“header”>
<span id=“logo”>Logo here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
phpXperts - 09
Basic Selector Example
$(“ul.menu”)
<body>
<div id=“header”>
<span id=“logo”>Logo here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
phpXperts - 09
Basic Selector Example
$(“ul.menu li”)
<body>
<div id=“header”>
<span id=“logo”>Logo here…</span>
<ul class=“menu”>
<li>user name</li>
…..
<li>logout</li>
</ul>
</div>
……
</body>
phpXperts - 09
Using filters for selecting
Basic Filters
 :first, :last, :even, :odd, …...
phpXperts - 09
Basic Filters Example
Name Class Roll No. Comment
Raju XII 2 Good
Masud IX 1 Good
Apu XII 3
Mizan XII 5
Karim VI 2 Satisfactory
Student list table. Lets make it zebra.
phpXperts - 09
Basic Filters Example
Name Class Roll No. Comment
Raju XII 2 Good
Masud IX 1 Good
Apu XII 3
Mizan XII 5
Karim VI 2 Satisfactory
$(“#students tr:even”).css(“background-color”, “#dde”)
phpXperts - 09
Using filters for selecting
Basic Filters
 :first, :last, :even, :odd, …...
Content Filters:
 :empty , :contains(text), :has(selector), …..
phpXperts - 09
Content Filters Example
Name Class Roll No. Comment
Raju XII 2 Good
Masud IX 1 Good
Apu XII 3 No Comment
Mizan XII 5 No Comment
Karim VI 2 Satisfactory
$(“#students tr:even”).css(“background-color”, “#dde”);
$(“#students td.comment:empty”).text(“No Comment”);
phpXperts - 09
Using filters for selecting
Basic Filters
 :first, :last, :even, :odd, …...
Content Filters:
 :empty , :contains(text), :has(selector), …..
Attribute Filters:
 [attribute], [attribute=value], [attribute!=value], …..
phpXperts - 09
Attribute Filters Example
Name Class Roll No. Comment
Raju XII 2 Good
Masud IX 1 Good
Apu XII 3 No Comment
Mizan XII 5 No Comment
Karim VI 2 Satisfactory
$(“#students tr:even”).css(“background-color”, “#dde”);
$(“#students td.comment:empty”).text(“No Comment”);
$(“#students td[align=‘center']").addClass(“ocean”);
phpXperts - 09
Using filters for selecting
Basic Filters
 :first, :last, :even, :odd, …...
Content Filters:
 :empty , :contains(text), :has(selector), …..
Attribute Filters:
 [attribute], [attribute=value], [attribute!=value], …..
Forms
 :input, :text, :submit, :password, …..
 :enabled, :disabled, :checked, …..
phpXperts - 09
Forms Selector Example
$(":submit").click(function(e){ … });
$("input:disabled").val(“You cannot change me");
$(“#form-id input:checked”).addClass(“selected”);
phpXperts - 09
Now we can Select
Let’s perform some action
phpXperts - 09
jQuery Methods
DOM Manipulation
 before(), after(), append(), appendTo(), …..
phpXperts - 09
Dom Manipulation Example
Move all paragraphs in div with id “contents”
$(“p”)
<body>
<h1>jQuery</h1>
<p>jQuery is good</p>
<p>jQuery is better</p>
<div id=“contents”></div>
<p>jQuery is the best</p>
</body>
phpXperts - 09
Dom Manipulation Example
Move all paragraphs in div with id “contents”
$(“p”).appendTo(“#contents”);
<body>
<h1>jQuery</h1>
<div id=“contents”>
<p>jQuery is good</p>
<p>jQuery is better</p>
<p>jQuery is the best</p>
</div>
</body>
phpXperts - 09
Dom Manipulation Example
Move all paragraphs in div with id “contents”
$(“p”).appendTo(“#contents”);
$(“h1”).append(“ Dom Manipulation”);
<body>
<h1>jQuery Dom Manipulation</h1>
<div id=“contents”>
<p>jQuery is good</p>
<p>jQuery is better</p>
<p>jQuery is the best</p>
</div>
</body>
phpXperts - 09
jQuery Methods
DOM Manipulation
 before(), after(), append(), appendTo(), …..
Attributes
 css(), addClass(), attr(), html(), val(), …..
phpXperts - 09
Attributes Example
Make the texts of last paragraph bold
$(“#contents p:last”).css(“color”, “green”);
<body>
<h1>jQuery Dom Manipulation</h1>
<div id=“contents”>
<p >jQuery is good</p>
<p>jQuery is better</p>
<p style=“color:green”>jQuery is the best</p>
</div>
</body>
phpXperts - 09
More Attributes Example
Setting
$(“img.logo”).attr(“align”, “left”);
$(“p.copyright”).html(“&copy; 2009 ajaxray”);
$(“input#name”).val(“Spiderman”);
phpXperts - 09
More Attributes Example
Setting
$(“img.logo”).attr(“align”, “left”);
$(“p.copyright”).html(“&copy; 2009 ajaxray”);
$(“input#name”).val(“Spiderman”);
Getting
var allignment = $(“img.logo”).attr(“align”);
var copyright = $(“p.copyright”).html();
var username = $(“input#name”).val();
phpXperts - 09
jQuery Methods
DOM Manipulation
 before(), after(), append(), appendTo(), …..
Attributes
 css(), addClass(), attr(), html(), val(), …..
Events
 click(), bind(), unbind(), live(), …..
phpXperts - 09
Event Example
Start when DOM is ready
$(document).ready(function(){
$(selector).eventName(function(){…});
});
phpXperts - 09
Event Example
Bind all interactions on events.
$(document).ready(function(){
$(“#message”).click(function(){
$(this).hide();
})
});
<span id=“message” onclick=“…”> blah blah </span>
phpXperts - 09
Event Example
You can fire events manually.
$(document).ready(function(){
$(“span#message”).click(function(){
$(this).hide();
})
$(“#form-id:reset”).click();
});
phpXperts - 09
jQuery Methods
DOM Manipulation
 before(), after(), append(), appendTo(), …..
Attributes
 css(), addClass(), attr(), html(), val(), …..
Events
 click(), bind(), unbind(), live(), …..
Effects
 hide(), fadeOut(), toggle(), animate(), …..
phpXperts - 09
Effects Example
When “show-cart” link clicked, slide up/down “cart” div.
$(“a#show-cart”).click(function(){
$(“#cart”).slideToggle(“slow”);
})
phpXperts - 09
Effects Example
Build your custom animation
$(“a#show-cart”).click(function(){
$(“#cart”).slideToggle(“slow”);
})
$("#showdown").click(function(){
$("#my-div").animate({
width: "70%",
opacity: 0.4,
fontSize: "3em“
}, 1200 );
});
phpXperts - 09
jQuery Methods
DOM Manipulation
 before(), after(), append(), appendTo(), …..
Attributes
 css(), addClass(), attr(), html(), val(), …..
Events
 click(), bind(), unbind(), live(), …..
Effects
 hide(), fadeOut(), toggle(), animate(), …..
Ajax
 load(), get(), ajax(), getJSON(), …..
phpXperts - 09
Ajax Examples
Load a page in a container
$(“#comments”).load(“/get_comments.php”);
$(“#comments”).load(“/get_comments.php”, {max : 5});
phpXperts - 09
Ajax Examples
Send ajax request with data
$.get(“/edit_comment.php",
{id: 102, comment: “I m edited"}
);
phpXperts - 09
Ajax Examples
You can send serialized form as data
$.get(“/edit_comment.php",
$(“#edit-comment”).serialize()
);
id=102&comment=I+m+edited
phpXperts - 09
Ajax Examples
Set a callback function for handling response data
$.get(“edit_comment.php",
$(“form#cmm-edit”).serialize(),
function(data){
if(data == “success”)
alert(“Comment Edited!”);
}
);
phpXperts - 09
Chaining Methods
Most jQuery methods return jQuery object
You can chain them together
phpXperts - 09
Chaining Methods
Most jQuery methods return jQuery object
You can chain them together
$(“#deleted”).addClass(“red”).fadeOut(“slow”);
$(“:button”).val(“Click Me”).click(function(){…})
phpXperts - 09
Chaining Methods
Most jQuery methods return jQuery object
You can chain them together
$(“#deleted”).addClass(“red”).fadeOut(“slow”);
$(“:button”).val(“Click Me”).click(function(){…})
This will not work -
$(“:button”).val().click(function(){…})
This method will return string
phpXperts - 09
jQuery Plugins
jQuery is extensible.
phpXperts - 09
jQuery Plugins
jQuery lightBox
http://leandrovieira.com/projects/jquery/lightbox/
phpXperts - 09
jQuery Plugins
Slider Plugins
http://www.hieu.co.uk/blog/index.php/imageswitch/
http://medienfreunde.com/lab/innerfade/
phpXperts - 09
And thousands of more…
http://plugins.jquery.com/
phpXperts - 09
jQuery UI
Build highly interactive web applications
phpXperts - 09
jQuery UI – Interactions
(Draggale, Droppable, Resizable, Selectable, Sortable)
phpXperts - 09
jQuery UI – Sortable Example
$("#items").sortable();
phpXperts - 09
jQuery UI – Widgets
(Accordion, Datepicker, Dialog, Progressbar, Slider, Tabs)
phpXperts - 09
jQuery UI – Datepicker Example
$("#date").datepicker();
phpXperts - 09
Which one will match your site?
phpXperts - 09
Designing a jQuery UI theme - Themeroller
http://ui.jquery.com/themeroller
phpXperts - 09
Anis uddin Ahmad
Sr. Software Engineer
Right Brain Solution Ltd.
http://ajaxray.com
phpXperts - 09
Questions?
phpXperts - 09
THANK YOU

Jquery tutorial

  • 1.
  • 2.
    phpXperts - 09 Whatis jQuery Javascript Library Fast and concise Simplifies the interaction between HTML and JavaScript
  • 3.
    phpXperts - 09 WhyjQuery? Lightweight : 19KB in size (Minified and Gzipped) CSS1 - 3 Complaint Cross Browser  (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome)
  • 4.
    phpXperts - 09 WhyjQuery? Lightweight : 19KB in size (Minified and Gzipped) CSS1 - 3 Complaint Cross Browser  (IE 6.0+, FF 2+, Safari 3.0+, Opera 9.0+, Chrome) Great Community Plugins Tutorials TestCoverage Open (free) license Books
  • 5.
    phpXperts - 09 Aajax and DOM manipulation example if (!document.ELEMENT_NODE) { document.ELEMENT_NODE = 1; document.ATTRIBUTE_NODE = 2; document.TEXT_NODE = 3; document.CDATA_SECTION_NODE = 4; document.ENTITY_REFERENCE_NODE = 5; document.ENTITY_NODE = 6; document.PROCESSING_INSTRUCTION_NODE = 7; document.COMMENT_NODE = 8; document.DOCUMENT_NODE = 9; document.DOCUMENT_TYPE_NODE = 10; document.DOCUMENT_FRAGMENT_NODE = 11; document.NOTATION_NODE = 12; } document._importNode = function(node, allChildren) { switch (node.nodeType) { case document.ELEMENT_NODE: var newNode = document.createElement(node » .nodeName); /* does the node have any attributes to add? */ if (node.attributes && node.attributes » .length > 0) for (var i = 0; il = node.attributes.length; » i < il) newNode.setAttribute(node.attributes » .nodeName, node.getAttribute(node.attributes[i++] » .nodeName)); /* are we going after children too, and does » the node have any? */ if (allChildren && node.childNodes && » node.childNodes.length > 0) for (var i = 0; il = node.childNodes.length; » i < il) newNode.appendChild(document._importNode » (node.childNodes[i++], allChildren)); return newNode; break; case document.TEXT_NODE: case document.CDATA_SECTION_NODE: case document.COMMENT_NODE: return document.createTextNode(node.nodeValue); break; } }; http://www.alistapart.com/articles/crossbrowserscripting
  • 6.
    phpXperts - 09 It’sjust a single line in jQuery $(“#content”).load(“page.html #content”);
  • 7.
    phpXperts - 09 Who’susing jQuery? http://docs.jquery.com/Sites_Using_jQuery
  • 8.
    phpXperts - 09 Dominatingthe world Google trends comparison of last JS framework 12 months http://www.google.com/trends?q=jQuery%2C+prototype%2C+yui%2C+dojo%2C+mootools&ctab=0&g eo=all&date=ytd&sort=0
  • 9.
    phpXperts - 09 Let’sStart Download jQuery http://docs.jquery.com/Downloading_jQuery
  • 10.
    phpXperts - 09 Embedin you page <html> <head> <script src=“path/to/jquery-x.x.js"></script> </head> <body> … </body> </html>
  • 11.
    phpXperts - 09 Embedin you page <html> <head> <script src="path/to/jquery-x.x.js"></script> <script> $(document).ready(function(){ // Start here }); </script> </head> <body> … </body> </html>
  • 12.
    phpXperts - 09 $(“div”).addClass(“xyz”); FindSome Elements Do something with them { }jQuery Object jQuery philosophy
  • 13.
    phpXperts - 09 ABasic Example <body> <div> <p>I m a paragraph 1</p> <p>I m a paragraph 2</p> </div> <p>I m another paragraph</p> </body>
  • 14.
    phpXperts - 09 ABasic Example <body> <div> <p>I m a paragraph 1</p> <p>I m a paragraph 2</p> </div> <p>I m another paragraph</p> </body> Select all paragraphs. $(“p”)
  • 15.
    phpXperts - 09 ABasic Example <body> <div> <p class=“red”>I m a paragraph -1</p> <p class=“red”>I m a paragraph -2</p> </div> <p class=“red”>I m another paragraph</p> </body> Select all paragraphs. Add a class to them. $(“p”).addClass(“red”);
  • 16.
    phpXperts - 09 SelectorBasics Just pass a selector to $() What is selector? Use any CSS selector
  • 17.
    phpXperts - 09 SelectorBasics Think about your simplest css file. #header{ margin : 0 auto; } div{ margin : 0px; padding : 0px } ul.menu li{ ….. }
  • 18.
    phpXperts - 09 SelectorBasics The red colored items are selectors #header{ margin : 0 auto; } div{ margin : 0px; padding : 0px } ul.menu li{ ….. }
  • 19.
    phpXperts - 09 SelectorBasics Selecting By Id  $(“#header”) Selecting using selectors
  • 20.
    phpXperts - 09 SelectorBasics Selecting By Id  $(“#header”) Selecting By Class  $(“.updated”) Selecting using selectors
  • 21.
    phpXperts - 09 SelectorBasics Selecting By Id  $(“#header”) Selecting By Class  $(“.updated”) Selecting by tag name  $(“table”) Selecting using selectors
  • 22.
    phpXperts - 09 SelectorBasics Selecting By Id  $(“#header”) Selecting By Class  $(“.updated”) Selecting by tag name  $(“table”) Combine them  $(“table.user-list”)  $(“#footer ul.menu li”) Selecting using selectors
  • 23.
    phpXperts - 09 BasicSelector Example This is my page <body> <div id=“header”> <span id=“logo”>Logo here…</span> <ul class=“menu”> <li>user name</li> ….. <li>logout</li> </ul> </div> …… </body>
  • 24.
    phpXperts - 09 BasicSelector Example $(“#header”) <body> <div id=“header”> <span id=“logo”>Logo here…</span> <ul class=“menu”> <li>user name</li> ….. <li>logout</li> </ul> </div> …… </body>
  • 25.
    phpXperts - 09 BasicSelector Example $(“ul.menu”) <body> <div id=“header”> <span id=“logo”>Logo here…</span> <ul class=“menu”> <li>user name</li> ….. <li>logout</li> </ul> </div> …… </body>
  • 26.
    phpXperts - 09 BasicSelector Example $(“ul.menu li”) <body> <div id=“header”> <span id=“logo”>Logo here…</span> <ul class=“menu”> <li>user name</li> ….. <li>logout</li> </ul> </div> …… </body>
  • 27.
    phpXperts - 09 Usingfilters for selecting Basic Filters  :first, :last, :even, :odd, …...
  • 28.
    phpXperts - 09 BasicFilters Example Name Class Roll No. Comment Raju XII 2 Good Masud IX 1 Good Apu XII 3 Mizan XII 5 Karim VI 2 Satisfactory Student list table. Lets make it zebra.
  • 29.
    phpXperts - 09 BasicFilters Example Name Class Roll No. Comment Raju XII 2 Good Masud IX 1 Good Apu XII 3 Mizan XII 5 Karim VI 2 Satisfactory $(“#students tr:even”).css(“background-color”, “#dde”)
  • 30.
    phpXperts - 09 Usingfilters for selecting Basic Filters  :first, :last, :even, :odd, …... Content Filters:  :empty , :contains(text), :has(selector), …..
  • 31.
    phpXperts - 09 ContentFilters Example Name Class Roll No. Comment Raju XII 2 Good Masud IX 1 Good Apu XII 3 No Comment Mizan XII 5 No Comment Karim VI 2 Satisfactory $(“#students tr:even”).css(“background-color”, “#dde”); $(“#students td.comment:empty”).text(“No Comment”);
  • 32.
    phpXperts - 09 Usingfilters for selecting Basic Filters  :first, :last, :even, :odd, …... Content Filters:  :empty , :contains(text), :has(selector), ….. Attribute Filters:  [attribute], [attribute=value], [attribute!=value], …..
  • 33.
    phpXperts - 09 AttributeFilters Example Name Class Roll No. Comment Raju XII 2 Good Masud IX 1 Good Apu XII 3 No Comment Mizan XII 5 No Comment Karim VI 2 Satisfactory $(“#students tr:even”).css(“background-color”, “#dde”); $(“#students td.comment:empty”).text(“No Comment”); $(“#students td[align=‘center']").addClass(“ocean”);
  • 34.
    phpXperts - 09 Usingfilters for selecting Basic Filters  :first, :last, :even, :odd, …... Content Filters:  :empty , :contains(text), :has(selector), ….. Attribute Filters:  [attribute], [attribute=value], [attribute!=value], ….. Forms  :input, :text, :submit, :password, …..  :enabled, :disabled, :checked, …..
  • 35.
    phpXperts - 09 FormsSelector Example $(":submit").click(function(e){ … }); $("input:disabled").val(“You cannot change me"); $(“#form-id input:checked”).addClass(“selected”);
  • 36.
    phpXperts - 09 Nowwe can Select Let’s perform some action
  • 37.
    phpXperts - 09 jQueryMethods DOM Manipulation  before(), after(), append(), appendTo(), …..
  • 38.
    phpXperts - 09 DomManipulation Example Move all paragraphs in div with id “contents” $(“p”) <body> <h1>jQuery</h1> <p>jQuery is good</p> <p>jQuery is better</p> <div id=“contents”></div> <p>jQuery is the best</p> </body>
  • 39.
    phpXperts - 09 DomManipulation Example Move all paragraphs in div with id “contents” $(“p”).appendTo(“#contents”); <body> <h1>jQuery</h1> <div id=“contents”> <p>jQuery is good</p> <p>jQuery is better</p> <p>jQuery is the best</p> </div> </body>
  • 40.
    phpXperts - 09 DomManipulation Example Move all paragraphs in div with id “contents” $(“p”).appendTo(“#contents”); $(“h1”).append(“ Dom Manipulation”); <body> <h1>jQuery Dom Manipulation</h1> <div id=“contents”> <p>jQuery is good</p> <p>jQuery is better</p> <p>jQuery is the best</p> </div> </body>
  • 41.
    phpXperts - 09 jQueryMethods DOM Manipulation  before(), after(), append(), appendTo(), ….. Attributes  css(), addClass(), attr(), html(), val(), …..
  • 42.
    phpXperts - 09 AttributesExample Make the texts of last paragraph bold $(“#contents p:last”).css(“color”, “green”); <body> <h1>jQuery Dom Manipulation</h1> <div id=“contents”> <p >jQuery is good</p> <p>jQuery is better</p> <p style=“color:green”>jQuery is the best</p> </div> </body>
  • 43.
    phpXperts - 09 MoreAttributes Example Setting $(“img.logo”).attr(“align”, “left”); $(“p.copyright”).html(“&copy; 2009 ajaxray”); $(“input#name”).val(“Spiderman”);
  • 44.
    phpXperts - 09 MoreAttributes Example Setting $(“img.logo”).attr(“align”, “left”); $(“p.copyright”).html(“&copy; 2009 ajaxray”); $(“input#name”).val(“Spiderman”); Getting var allignment = $(“img.logo”).attr(“align”); var copyright = $(“p.copyright”).html(); var username = $(“input#name”).val();
  • 45.
    phpXperts - 09 jQueryMethods DOM Manipulation  before(), after(), append(), appendTo(), ….. Attributes  css(), addClass(), attr(), html(), val(), ….. Events  click(), bind(), unbind(), live(), …..
  • 46.
    phpXperts - 09 EventExample Start when DOM is ready $(document).ready(function(){ $(selector).eventName(function(){…}); });
  • 47.
    phpXperts - 09 EventExample Bind all interactions on events. $(document).ready(function(){ $(“#message”).click(function(){ $(this).hide(); }) }); <span id=“message” onclick=“…”> blah blah </span>
  • 48.
    phpXperts - 09 EventExample You can fire events manually. $(document).ready(function(){ $(“span#message”).click(function(){ $(this).hide(); }) $(“#form-id:reset”).click(); });
  • 49.
    phpXperts - 09 jQueryMethods DOM Manipulation  before(), after(), append(), appendTo(), ….. Attributes  css(), addClass(), attr(), html(), val(), ….. Events  click(), bind(), unbind(), live(), ….. Effects  hide(), fadeOut(), toggle(), animate(), …..
  • 50.
    phpXperts - 09 EffectsExample When “show-cart” link clicked, slide up/down “cart” div. $(“a#show-cart”).click(function(){ $(“#cart”).slideToggle(“slow”); })
  • 51.
    phpXperts - 09 EffectsExample Build your custom animation $(“a#show-cart”).click(function(){ $(“#cart”).slideToggle(“slow”); }) $("#showdown").click(function(){ $("#my-div").animate({ width: "70%", opacity: 0.4, fontSize: "3em“ }, 1200 ); });
  • 52.
    phpXperts - 09 jQueryMethods DOM Manipulation  before(), after(), append(), appendTo(), ….. Attributes  css(), addClass(), attr(), html(), val(), ….. Events  click(), bind(), unbind(), live(), ….. Effects  hide(), fadeOut(), toggle(), animate(), ….. Ajax  load(), get(), ajax(), getJSON(), …..
  • 53.
    phpXperts - 09 AjaxExamples Load a page in a container $(“#comments”).load(“/get_comments.php”); $(“#comments”).load(“/get_comments.php”, {max : 5});
  • 54.
    phpXperts - 09 AjaxExamples Send ajax request with data $.get(“/edit_comment.php", {id: 102, comment: “I m edited"} );
  • 55.
    phpXperts - 09 AjaxExamples You can send serialized form as data $.get(“/edit_comment.php", $(“#edit-comment”).serialize() ); id=102&comment=I+m+edited
  • 56.
    phpXperts - 09 AjaxExamples Set a callback function for handling response data $.get(“edit_comment.php", $(“form#cmm-edit”).serialize(), function(data){ if(data == “success”) alert(“Comment Edited!”); } );
  • 57.
    phpXperts - 09 ChainingMethods Most jQuery methods return jQuery object You can chain them together
  • 58.
    phpXperts - 09 ChainingMethods Most jQuery methods return jQuery object You can chain them together $(“#deleted”).addClass(“red”).fadeOut(“slow”); $(“:button”).val(“Click Me”).click(function(){…})
  • 59.
    phpXperts - 09 ChainingMethods Most jQuery methods return jQuery object You can chain them together $(“#deleted”).addClass(“red”).fadeOut(“slow”); $(“:button”).val(“Click Me”).click(function(){…}) This will not work - $(“:button”).val().click(function(){…}) This method will return string
  • 60.
    phpXperts - 09 jQueryPlugins jQuery is extensible.
  • 61.
    phpXperts - 09 jQueryPlugins jQuery lightBox http://leandrovieira.com/projects/jquery/lightbox/
  • 62.
    phpXperts - 09 jQueryPlugins Slider Plugins http://www.hieu.co.uk/blog/index.php/imageswitch/ http://medienfreunde.com/lab/innerfade/
  • 63.
    phpXperts - 09 Andthousands of more… http://plugins.jquery.com/
  • 64.
    phpXperts - 09 jQueryUI Build highly interactive web applications
  • 65.
    phpXperts - 09 jQueryUI – Interactions (Draggale, Droppable, Resizable, Selectable, Sortable)
  • 66.
    phpXperts - 09 jQueryUI – Sortable Example $("#items").sortable();
  • 67.
    phpXperts - 09 jQueryUI – Widgets (Accordion, Datepicker, Dialog, Progressbar, Slider, Tabs)
  • 68.
    phpXperts - 09 jQueryUI – Datepicker Example $("#date").datepicker();
  • 69.
    phpXperts - 09 Whichone will match your site?
  • 70.
    phpXperts - 09 Designinga jQuery UI theme - Themeroller http://ui.jquery.com/themeroller
  • 71.
    phpXperts - 09 Anisuddin Ahmad Sr. Software Engineer Right Brain Solution Ltd. http://ajaxray.com
  • 72.
  • 73.