Jquery Presentation
Jquery Presentation
- 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
<html>
<head>
<script src="jquery.js"></script>
</head>
<body>
</body>
</html>
jQuery Syntax
With jQuery you select (query) HTML elements and
perform "actions" on them.
Basic syntax is: $(selector).action()
A $ sign to define/access jQuery
A (selector) HTML elements
A jQuery action() to be performed on the
element(s)
Examples:
$("p").hide() - hides all <p> elements.
$("#test").hide() - hides the element with id="test".
$(".test").hide() - hides all elements with
class="test".
The Document Ready Event
$(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