Jquery Presentation Final
Jquery Presentation Final
- A JavaScript Library
By : Anita M. Rooge
What is Jquery ?
JQuery is a lightweight, "write less, do
more", JavaScript library.
Go to http://docs.jquery.com/Main_Page
OR
Go to https://
ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
Use following code snippet
jQuery Syntax
$(selector).action()
$(document).ready(function(){
});
$(document).ready(function(){
$(“#button").click(function(){
$("p").hide();
});
});
HTML Tag <P> is specified as element here
Written as <p> text goes here</p>
Id (#) Selector
The jQuery #id selector uses the id attribute of
an HTML tag to find the specific element.
Example :
$(document).ready(function(){
$(“#bt1").click(function(){
$("#test").hide();
});
});
#button and #test used as id here. The html tags
used as
<input type =“button” id = “bt1” value =“Click”>
<p id = “test”>Para txt goes here </p>
Class (.) Selector
The jQuery class selector finds elements with a
specific class.
Example :
$(document).ready(function(){
$(“#button").click(function(){
$(".test").hide();
});
});
.test used as class here. The html tags used as
<p class = “test”>Para txt goes here </p>
More Jquery Selectors
Syntax Description
$("*") Selects all elements
$("ul li:first") Selects the first <li> element of the first <ul>
Syntax :
$(selector).hide(speed, callback);
$(selector).show(speed, callback);
$(selector).toggle(speed, callback);
Example : hodeshow.html
Jquery Fading
Methods
With jQuery you can fade an element in and out of visibility.
jQuery has the following fade methods:
Syntax :
$(selector).fadeIn(speed, callback);
$(selector).fadeOut(speed, callback);
$(selector).fadeToggle(speed, callback);
$(selector).fadeTo(speed, callback);
Example : fadeinout1.html
Jquery Effects : Sliding
With jQuery you can create a sliding effect on elements.
jQuery has the following slide methods :
Syntax :
$(selector).slideDown(speed, callback);
$(selector). slideUp(speed, callback);
$(selector).slideToggle(speed, callback);
Example : slide.html
Jquery Effects :
Animation
The jQuery animate() method lets you create custom
animations.
Syntax :
$(selector).animate( { params }, speed, callback);
Here is the description of all the parameters used by this
method
params − A map of CSS properties that the animation will
move toward.
speed − This is optional parameter representing how long
the animation will run.
callback − This is optional parameter representing a
function to call once the animation is complete
Example : animate.html
Jquery Effects :Stop
The jQuery stop() method is used to stop an
animation or effect before it is finished.
The stop() method works for all jQuery effect
functions, including sliding, fading and custom
animations.
Syntax :
$(selector).stop(stopAll , goToEnd );
The optional stopAll Boolean parameter specifies
whether to remove queued animation or not. Default
value is false, that means only the current animation
will be stopped, rest of the animations in the queue will
run afterwards.
The optional goToEnd Boolean parameter specifies
whether to complete the current animation
immediately. Default value is false.
Jquery : Callback
A callback function is executed after the current effect is 100%
finished.
JavaScript statements are executed line by line. However, with
effects, the next line of code can be run even though the effect is
not finished. This can create errors.
To prevent this, you can create a callback function.
Syntax :
$(selector).hide(speed, callback);
Example : callback1.html
Jquery : chaining
The jQuery provides another robust feature called method
chaining that allows us to perform multiple action on the same set
of elements, all within a single line of code.
This is possible because most of the jQuery methods return a
jQuery object that can be further used to call another method.
Syntax :
$(selector).hide(speed, callback);
Example : chain.html
Jquery : Get & Set Content &
Attributes
jQuery contains powerful methods for changing and manipulating
HTML elements and attributes.
Three simple, but useful, jQuery methods for DOM manipulation is:
text() - Sets or returns the text content of selected elements
html() - Sets or returns the content of selected elements (including
HTML markup)
val() - Sets or returns the value of form fields
Syntax :
Example : contentattributes.html
jQuery text() Method : get text
The jQuery text() method is either used to get the
combined text contents of the selected elements,
including their descendants
Getattr.html
Setattr.html
jQuery val() Method
You can use the jQuery val() method to
either get the value of an form elements or
set value for selected form element.
getsetval.html
Jquery : Add remove
Elements/Content
With jQuery, it is easy to add new elements/content.
append() : The jQuery append() method is used to
Example :appdel.html
jquery - Get and Set CSS Classes
Query provides several methods as follows to
manipulate the CSS classes assigned to HTML
elements.
addClass() - Adds one or more classes to the
selected elements
removeClass() - Removes one or more classes
from the selected elements
toggleClass() - Toggles between adding/removing
classes
Example : addclass.html
jQuery CSS() Method
The jQuery CSS() method is used to get (return)or
set style properties or values for selected
elements. It facilitates you to get one or more
style properties.
Syntax:
Eg:
alert("Background color = " + $("p").css("background-color"));
$("p").css("background-color", "violet");
jQuery noConflict() Method
The $ (dollar) sign in jQuery is used as an alias or
shortcut. Many JavaScript libraries also use the $ sign as
the shortcut, along with the jQuery on the same page. But
if two different frameworks use the same alias, a conflict
could occur, and one of the frameworks might stop its
working.
The noConflict() method implemented in jQuery to deal
with such situations. It is used to avoid the conflict of
using $ variable with other libraries. This method releases
the hold on the $ (dollar) identifier so that other libraries
can use it.
When the document is loaded, jQuery goes into noConflict
mode. Here, we are creating a new alias using the
jQuery noConflict(). We are declaring a variable named
as j and assigning the noConflict() method to it. Now, this
variable will work as an alias for the jQuery code. This new
alias will be used instead of using $. So, it avoids conflict
jQuery noConflict Method
var j = $.noConflict();
j(document).ready(function(){
j("#b1").click(function(){
alert("jQuery is working fine");
});
});
AJAX
• It stands for “Asynchronous JavaScript and XML”
• It was first mentioned by Jesse James Garrett in 2005. [1]
• It is not a new technology. It is several technologies. [1]
standards-based presentation: XHTML and CSS
dynamic display and interaction: DOM
data interchange and manipulation: XML and XSLT
asynchronous data retrieval: XMLHttpRequest
binding everything together: JavaScript
• Intuition: Static web site vs. AJAX site
$.ajax(url,[options]);