This jQuery Tutorial from http://www.w3schools.com
$("#test").hide()
Demonstrates the jQuery hide() method, hiding the element with id="test".
$("p").hide()
Demonstrates the jQuery hide() method, hiding all <p> elements.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
$(".test").hide()
Demonstrates the jQuery hide() method, hiding all elements with class="test".
jQuery Effects
Both hide() and show() can take the two optional parameters: speed and callback.
Syntax:
$(selector).hide(speed,callback)
$(selector).show(speed,callback)
The speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", "normal", or milliseconds:
jQuery Callback Functions
jQuery HTML Manipulation
Changing HTML Content
$(selector).html(content)
The html() method changes the contents (innerHTML) of matching HTML elements.
Adding HTML content
$(selector).append(content)
The append() method appends content to the inside of matching HTML elements.
$(selector).prepend(content)
The prepend() method "prepends" content to the inside of matching HTML elements.
$(selector).after(content)
The after() method inserts HTML content after all matching elements.
$(selector).before(content)
The before() method inserts HTML content before all matching elements.
jQuery HTML Manipulation Methods From This Page:
jQuery CSS Manipulation
jQuery AJAX
jQuery is a JavaScript Library.
jQuery greatly simplifies JavaScript programming.
jQuery is easy to learn.
The jQuery library can be added to a web page with a single line of markup.
What is jQuery?
jQuery is a library of JavaScript Functions.
jQuery is a lightweight "write less, do more" JavaScript library.
The jQuery library contains the following features:
- HTML element selections
- HTML element manipulation
- CSS manipulation
- HTML event functions
- JavaScript Effects and animations
- HTML DOM traversal and modification
- AJAX
- Utilities
Adding the jQuery Library to Your Pages
The jQuery library is stored as a single JavaScript file, containing all the jQuery methods.
It can be added to a web page with the following mark-up:
<head> <script type="text/javascript" src="jquery.js"></script> </head> |
Please note that the <script> tag should be inside the page's <head> section.
Basic jQuery Example
The following example demonstrates the jQuery hide() method, hiding all <p> elements in an HTML document.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
Downloading jQuery
Two versions of jQuery are available for downloading: one minified and one uncompressed (for debugging or reading).
Alternatives to Downloading
If you don't want to store the jQuery library on your own computer, you can use the hosted jQuery library from Google or Microsoft.
Google:
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
Microsoft
<head> <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script> </head> |
With jQuery you select (query) HTML elements and perform "actions" on them.
jQuery Syntax Examples
$(this).hide()
Demonstrates the jQuery hide() method, hiding the current HTML element.
Demonstrates the jQuery hide() method, hiding the current HTML element.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<button>Click me</button>
</body>
</html>
$("#test").hide()
Demonstrates the jQuery hide() method, hiding the element with id="test".
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p id="test">This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
Demonstrates the jQuery hide() method, hiding all <p> elements.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
$(".test").hide()
Demonstrates the jQuery hide() method, hiding all elements with class="test".
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
</script>
</head>
<body>
<h2 class="test">This is a heading</h2>
<p class="test">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
jQuery Syntax
The jQuery syntax is tailor made for selecting HTML elements and perform some action on the element(s).
Basic syntax is: $(selector).action()
- A dollar sign to define jQuery
- A (selector) to "query (or find)" HTML elements
- A jQuery action() to be performed on the element(s)
Examples:
$(this).hide() - hides current element
$("p").hide() - hides all paragraphs
$("p.test").hide() - hides all paragraphs with class="test"
$("#test").hide() - hides the element with id="test"
The Document Ready Function
You might have noticed that all jQuery methods, in our examples, are inside a document.ready() function:
$(document).ready(function(){
// jQuery functions go here...
});
// jQuery functions go here...
});
This is to prevent any jQuery code from running before the document is finished loading (is ready).
Here are some examples of actions that can fail if functions are run before the document is fully loaded:
- Trying to hide an element that doesn't exist
- Trying to get the size of an image that is not loaded
jQuery selectors allow you to select and manipulate HTML elements as a group or as a single element.
jQuery Selectors
In the previous chapter we looked at some examples of how to select different HTML elements.
It is a key point to learn how jQuery selects exactly the elements you want to apply an effect to.
jQuery selectors allow you to select HTML elements (or groups of elements) by element name, attribute name or by content.
jQuery Element Selectors
jQuery uses CSS selectors to select HTML elements.
$("p") selects all <p> elements.
$("p.intro") selects all <p> elements with class="intro".
$("p#demo") selects all <p> elements with id="demo".
jQuery Attribute Selectors
jQuery uses XPath expressions to select elements with given attributes.
$("[href]") select all elements with an href attribute.
$("[href='#']") select all elements with an href value equal to "#".
$("[href!='#']") select all elements with an href attribute NOT equal to "#".
$("[href$='.jpg']") select all elements with an href attribute that ends with ".jpg".
jQuery CSS Selectors
jQuery CSS selectors can be used to change CSS properties for HTML elements.
The following example changes the background-color of all p elements to yellow:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").css("background-color","yellow");
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
Some More Examples
Syntax | Description |
$(this) | Current HTML element |
$("p") | All <p> elements |
$("p.intro") | All <p> elements with class="intro" |
$("p#intro") | All <p> elements with id="intro" |
$("p#intro:first") | The first <p> element with id="intro" |
$(".intro") | All elements with class="intro" |
$("#intro") | The first element with id="intro" |
$("ul li:first") | The first <li> element of each <ul> |
$("[href$='.jpg']") | All elements with an href attribute that ends with ".jpg" |
$("div#intro .head") | All elements with class="head" inside a <div> element with id="intro" |
Selector | Example | Selects |
$("*") | All elements | |
$("#lastname") | The element with id=lastname | |
$(".intro") | All elements with class="intro" | |
$("p") | All p elements | |
.class.class | $(".intro.demo") | All elements with the classes "intro" and "demo" |
$("p:first") | The first p element | |
$("p:last") | The last p element | |
$("tr:even") | All even tr elements | |
$("tr:odd") | All odd tr elements | |
$("ul li:eq(3)") | The fourth element in a list (index starts at 0) | |
$("ul li:gt(3)") | List elements with an index greater than 3 | |
$("ul li:lt(3)") | List elements with an index less than 3 | |
$("input:not(:empty)") | All input elements that are not empty | |
$(":header") | All header elements h1, h2 ... | |
$(":animated") | All animated elements | |
$(":contains('W3Schools')") | All elements which contains the text | |
$(":empty") | All elements with no child (elements) nodes | |
:hidden | $("p:hidden") | All hidden p elements |
$("table:visible") | All visible tables | |
s1,s2,s3 | $("th,td,.intro") | All elements with matching selectors |
$("[href]") | All elements with a href attribute | |
$("[href='default.htm']") | All elements with a href attribute value equal to "default.htm" | |
$("[href!='default.htm']") | All elements with a href attribute value not equal to "default.htm" | |
$("[href$='.jpg']") | All elements with a href attribute value ending with ".jpg" | |
$(":input") | All input elements | |
$(":text") | All input elements with type="text" | |
$(":password") | All input elements with type="password" | |
$(":radio") | All input elements with type="radio" | |
$(":checkbox") | All input elements with type="checkbox" | |
$(":submit") | All input elements with type="submit" | |
$(":reset") | All input elements with type="reset" | |
$(":button") | All input elements with type="button" | |
$(":image") | All input elements with type="image" | |
$(":file") | All input elements with type="file" | |
$(":enabled") | All enabled input elements | |
$(":disabled") | All disabled input elements | |
$(":selected") | All selected input elements | |
$(":checked") | All checked input elements |
jQuery is tailor made to handle events.
jQuery Event Functions
The jQuery event handling methods are core functions in jQuery.
Event handlers are method that are called when "something happens" in HTML. The term "triggered (or "fired") by an event" is often used.
It is common to put jQuery code into event handler methods in the <head> section:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
In the example above, a function is called when the click event for the button is triggered:
$("button").click(function() {..some code... } )
The method hides all <p> elements:
$("p").hide();
Functions In a Separate File
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain, put your jQuery functions in a separate .js file.
When we demonstrate jQuery here, the functions are added directly into the <head> section, However, sometimes it is preferable to place them in a separate file, like this (refer to the file with the src attribute):
Example
|
jQuery Name Conflicts
jQuery uses the $ sign as a shortcut for jQuery.
Some other JavaScript libraries also use the dollar sign for their functions.
The jQuery noConflict() method specifies a custom name (like jq), instead of using the dollar sign.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var jq=jQuery.noConflict();
jq(document).ready(function(){
jq("button").click(function(){
jq("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
jQuery Events
Here are some examples of event methods in jQuery:
Event Method | Description |
$(document).ready(function) | Binds a function to the ready event of a document (when the document is finished loading) |
$(selector).click(function) | Triggers, or binds a function to the click event of selected elements |
$(selector).dblclick(function) | Triggers, or binds a function to the double click event of selected elements |
$(selector).focus(function) | Triggers, or binds a function to the focus event of selected elements |
$(selector).mouseover(function) | Triggers, or binds a function to the mouseover event of selected elements |
jQuery Event Methods
Event methods trigger, or bind a function to an event for all matching elements.
Trigger example:
$("button").click() - triggers the click event for a button element.
Binding example:
$("button").click(function(){$("img").hide()}) - binds a function to the click event.
The following table lists all the methods used to handle events.
Method | Description |
Add one or more event handlers to matching elements | |
Triggers, or binds a function to the blur event of selected elements | |
Triggers, or binds a function to the change event of selected elements | |
Triggers, or binds a function to the click event of selected elements | |
Triggers, or binds a function to the dblclick event of selected elements | |
Add one or more event handlers to current, or future, specified child elements of the matching elements | |
Remove all event handlers added with the live() function | |
Triggers, or binds a function to the error event of selected elements | |
event.currentTarget | The current DOM element within the event bubbling phase |
event.data | Contains the optional data passed to jQuery.fn.bind when the current executing handler was bound |
Returns whether event.preventDefault() was called for the event object | |
event.isImmediatePropagationStopped() | Returns whether event.stopImmediatePropagation() was called for the event object |
event.isPropagationStopped() | Returns whether event.stopPropagation() was called for the event object |
The mouse position relative to the left edge of the document | |
The mouse position relative to the top edge of the document | |
Prevents the default action of the event | |
event.relatedTarget | The other DOM element involved in the event, if any |
This attribute contains the last value returned by an event handler that was triggered by this event, unless the value was undefined | |
event.stopImmediatePropagation() | Prevents other event handlers from being called |
event.stopPropagation() | Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event |
The DOM element that initiated the event | |
This attribute returns the number of milliseconds since January 1, 1970, when the event is triggered | |
Describes the nature of the event | |
Which key or button was pressed for a key or button event | |
Triggers, or binds a function to the focus event of selected elements | |
Binds a function to the focusin event of selected elements | |
Binds a function to the focusout event of selected elements | |
Binds one or two functions to the hover event of selected elements | |
Triggers, or binds a function to the keydown event of selected elements | |
Triggers, or binds a function to the keypress event of selected elements | |
Triggers, or binds a function to the keyup event of selected elements | |
Add one or more event handlers to current, or future, matching elements | |
Triggers, or binds a function to the load event of selected elements | |
Triggers, or binds a function to the mouse down event of selected elements | |
Triggers, or binds a function to the mouse enter event of selected elements | |
Triggers, or binds a function to the mouse leave event of selected elements | |
Triggers, or binds a function to the mouse move event of selected elements | |
Triggers, or binds a function to the mouse out event of selected elements | |
Triggers, or binds a function to the mouse over event of selected elements | |
Triggers, or binds a function to the mouse up event of selected elements | |
Add one or more event handlers to matching elements. This handler can only be triggered once per element | |
Binds a function to the ready event of a document (when an HTML document is ready to use) | |
Triggers, or binds a function to the resize event of selected elements | |
Triggers, or binds a function to the scroll event of selected elements | |
Triggers, or binds a function to the select event of selected elements | |
Triggers, or binds a function to the submit event of selected elements | |
Binds two or more functions to the toggle between for the click event for selected elements | |
Triggers all events bound to the selected elements | |
Triggers all functions bound to a specified event for the selected elements | |
Remove an added event handler from selected elements | |
Remove an event handler to selected elements, now or in the future | |
Triggers, or binds a function to the unload event of selected elements |
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").bind("click",function(){
$("p").slideToggle();
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>Click me!</button>
</body>
</html>
jQuery Effects
Hide, Show, Toggle, Slide, Fade, and Animate.
Examples
jQuery hide()
Demonstrates a simple jQuery hide() method.
Demonstrates a simple jQuery hide() method.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
jQuery hide()
Another hide() demonstration. How to hide parts of text.
Another hide() demonstration. How to hide parts of text.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".ex .hide").click(function(){
$(this).parents(".ex").hide("slow");
});
});
</script>
<style type="text/css">
div.ex
{
background-color:#e5eecc;
padding:7px;
border:solid 1px #c3c3c3;
}
</style>
</head>
<body>
<h3>Island Trading</h3>
<div class="ex">
<button class="hide">Hide me</button>
<p>Contact: Helen Bennett<br />
Garden House Crowther Way<br />
London</p>
</div>
<h3>Paris spécialités</h3>
<div class="ex">
<button class="hide">Hide me</button>
<p>Contact: Marie Bertrand<br />
265, Boulevard Charonne<br />
Paris</p>
</div>
</body>
</html>
jQuery slideToggle()
Demonstrates a simple slide panel effect.
Demonstrates a simple slide panel effect.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideToggle("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>
<p class="flip">Show/Hide Panel</p>
</body>
</html>
jQuery fadeTo()
Demonstrates a simple jQuery fadeTo() method.
Demonstrates a simple jQuery fadeTo() method.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").fadeTo("slow",0.25);
});
});
</script>
</head>
<body>
<div style="background:yellow;width:300px;height:300px">
<button>Click to Fade</button>
</div>
</body>
</html>
jQuery animate()
Demonstrates a simple jQuery animate() method.
Demonstrates a simple jQuery animate() method.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").animate({height:300},"slow");
$("div").animate({width:300},"slow");
$("div").animate({height:100},"slow");
$("div").animate({width:100},"slow");
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<br /><br />
<div style="background:#98bf21;height:100px;width:100px;position:relative">
</div>
</body>
</html>
jQuery Hide and Show
With jQuery, you can hide and show HTML elements with the hide() and show() methods:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>If you click on the "Hide" button, I will disappear.</p>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
Syntax:
$(selector).hide(speed,callback)
$(selector).show(speed,callback)
The speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", "normal", or milliseconds:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
});
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>
The callback parameter is the name of a function to be executed after the hide (or show) function completes. You will learn more about the callback parameter in the next chapter of this tutorial.
jQuery Toggle
The jQuery toggle() method toggles the visibility of HTML elements using the show() or hide() methods.
Shown elements are hidden and hidden elements are shown.
Syntax:
$(selector).toggle(speed,callback)
The speed parameter can take the following values: "slow", "fast", "normal", or milliseconds.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<button>Toggle</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>
The callback parameter is the name of a function to be executed after the hide (or show) method completes.
jQuery Slide - slideDown, slideUp, slideToggle
The jQuery slide methods gradually change the height for selected elements.
jQuery has the following slide methods:
$(selector).slideDown(speed,callback)
$(selector).slideUp(speed,callback)
$(selector).slideToggle(speed,callback)
The speed parameter can take the following values: "slow", "fast", "normal", or milliseconds.
The callback parameter is the name of a function to be executed after the function completes.
slideDown() Example
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideDown("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>
<p class="flip">Show Panel</p>
</body>
</html>
slideUp() Example
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideUp("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
}
</style>
</head>
<body>
<div class="panel">
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>
<p class="flip">Hide Panel</p>
</body>
</html>
slideToggle() Example
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".flip").click(function(){
$(".panel").slideToggle("slow");
});
});
</script>
<style type="text/css">
div.panel,p.flip
{
margin:0px;
padding:5px;
text-align:center;
background:#e5eecc;
border:solid 1px #c3c3c3;
}
div.panel
{
height:120px;
display:none;
}
</style>
</head>
<body>
<div class="panel">
<p>Because time is valuable, we deliver quick and easy learning.</p>
<p>At W3Schools, you can study everything you need to learn, in an accessible and handy format.</p>
</div>
<p class="flip">Show/Hide Panel</p>
</body>
</html>
jQuery Fade - fadeIn, fadeOut, fadeTo
The jQuery fade methods gradually change the opacity for selected elements.
jQuery has the following fade methods:
$(selector).fadeIn(speed,callback)
$(selector).fadeOut(speed,callback)
$(selector).fadeTo(speed,opacity,callback)
The speed parameter can take the following values: "slow", "fast", "normal", or milliseconds.
The opacity parameter in the fadeTo() method allows fading to a given opacity.
The callback parameter is the name of a function to be executed after the function completes.
fadeTo() Example
|
fadeOut() Example
|
jQuery Custom Animations
The syntax of jQuery's method for making custom animations is:
$(selector).animate({params},[duration],[easing],[callback])
The key parameter is params. It defines the CSS properties that will be animated. Many properties can be animated at the same time:
animate({width:"70%",opacity:0.4,marginLeft:"0.6in",fontSize:"3em"}); |
The second parameter is duration. It specifies the speed of the animation. Possible values are "fast", "slow", "normal", or milliseconds.
Example 1
|
Example 2
|
HTML elements are positioned static by default and cannot be moved. To make elements moveable, set the CSS position property to fixed, relative or absolute. |
jQuery Effects
Here are some examples of effect functions in jQuery:
Function | Description |
$(selector).hide() | Hide selected elements |
$(selector).show() | Show selected elements |
$(selector).toggle() | Toggle (between hide and show) selected elements |
$(selector).slideDown() | Slide-down (show) selected elements |
$(selector).slideUp() | Slide-up (hide) selected elements |
$(selector).slideToggle() | Toggle slide-up and slide-down of selected elements |
$(selector).fadeIn() | Fade in selected elements |
$(selector).fadeOut() | Fade out selected elements |
$(selector).fadeTo() | Fade out selected elements to a given opacity |
$(selector).animate() | Run a custom animation on selected elements |
jQuery Effect Methods
The following table lists all the methods used to create animation effects.
Method | Description |
Performs a custom animation (of a set of CSS properties) for selected elements | |
Removes all queued functions for the selected element | |
delay() | Sets a delay for all queued functions for the selected element |
dequeue() | Runs the next queued functions for the selected element |
Gradually changes the opacity, for selected elements, from hidden to visible | |
Gradually changes the opacity, for selected elements, from visible to hidden | |
Gradually changes the opacity, for selected elements, to a specified opacity | |
fadeToggle() | |
Hides selected elements | |
queue() | Shows the queued functions for the selected element |
Shows hidden selected elements | |
Gradually changes the height, for selected elements, from hidden to visible | |
Toggles between slideUp() and slideDown() for selected elements | |
Gradually changes the height, for selected elements, from visible to hidden | |
Stops a running animation on selected elements | |
Toggles between hide() and show(), or custom functions, for selected elements |
jQuery Callback Functions
A callback function is executed after the current animation is 100% finished.
jQuery Callback Functions
A callback function is executed after the current animation (effect) is finished.
JavaScript statements are executed line by line. However, with animations, the next line of code can be run even though the animation is not finished. This can create errors.
To prevent this, you can create a callback function. The callback function will not be called until after the animation is finished.
jQuery Callback Example
Typical syntax: $(selector).hide(speed,callback)
The callback parameter is a function to be executed after the hide effect is completed:
Example with Callback
|
Without a callback parameter, the alert box is displayed before the hide effect is completed:
Example without Callback
|
jQuery HTML Manipulation
jQuery contains powerful methods (functions) for changing and manipulating HTML elements and attributes.
Changing HTML Content
$(selector).html(content)
The html() method changes the contents (innerHTML) of matching HTML elements.Example
|
Adding HTML content
$(selector).append(content)The append() method appends content to the inside of matching HTML elements.
$(selector).prepend(content)
The prepend() method "prepends" content to the inside of matching HTML elements.
Example
|
The after() method inserts HTML content after all matching elements.
$(selector).before(content)
The before() method inserts HTML content before all matching elements.
Example
|
jQuery HTML Manipulation Methods From This Page:
Function | Description |
$(selector).html(content) | Changes the (inner) HTML of selected elements |
$(selector).append(content) | Appends content to the (inner) HTML of selected elements |
$(selector).after(content) | Adds HTML after selected elements |
jQuery HTML Methods
The following table lists all the methods used to manipulate the DOM.
The methods below work for both HTML and XML documents. Exception: the html() method.
Method | Description |
Adds one or more classes (for CSS) to selected elements | |
Inserts content after selected elements | |
Inserts content at the end of (but still inside) selected elements | |
Inserts content at the end of (but still inside) selected elements | |
Sets or returns an attribute and value of selected elements | |
Inserts content before selected elements | |
Makes a copy of selected elements | |
Removes (but keeps a copy of) selected elements | |
Removes all child elements and content from selected elements | |
Checks if any of the selected elements have a specified class (for CSS) | |
Sets or returns the content of selected elements | |
Inserts HTML markup or elements after selected elements | |
Inserts HTML markup or elements before selected elements | |
Inserts content at the beginning of (but still inside) selected elements | |
Inserts content at the beginning of (but still inside) selected elements | |
Removes selected elements | |
Removes an attribute from selected elements | |
Removes one or more classes (for CSS) from selected elements | |
Replaces selected elements with new content | |
Replaces selected elements with new content | |
Sets or returns the text content of selected elements | |
Toggles between adding/removing one or more classes (for CSS) from selected elements | |
Removes the parent element of the selected elements | |
Sets or returns the value attribute of the selected elements (form elements) | |
Wraps specified HTML element(s) around each selected element | |
Wraps specified HTML element(s) around all selected elements | |
Wraps specified HTML element(s) around the content of each selected element |
jQuery CSS Manipulation
jQuery css() Method
jQuery has one important method for CSS manipulation: css()
The css() method has three different syntaxes, to perform different tasks.
- css(name) - Return CSS property value
- css(name,value) - Set CSS property and value
- css({properties}) - Set multiple CSS properties and values
Return CSS Property
Use css(name) to return the specified CSS property value of the FIRST matched element:
Example
|
Set CSS Property and Value
Use css(name,value) to set the specified CSS property for ALL matched elements:
Example
|
Set Multiple CSS Property/Value Pairs
Use css({properties}) to set one or more CSS property/value pairs for the selected elements:
Example
|
jQuery height() and width() Methods
jQuery has two important methods for size manipulation.
- height()
- width()
Size Manipulation Examples
The height() method sets the height of all matching elements:
Example
|
The width() method sets the width of all matching elements:
Example
|
jQuery CSS Methods From this Page:
CSS Properties | Description |
$(selector).css(name) | Get the style property value of the first matched element |
$(selector).css(name,value) | Set the value of one style property for matched elements |
$(selector).css({properties}) | Set multiple style properties for matched elements |
$(selector).height(value) | Set the height of matched elements |
$(selector).width(value) | Set the width of matched elements |
jQuery CSS Methods
The following table lists all the methods used to manipulate CSS properties.
Method | Description |
Adds one or more classes to selected elements | |
Sets or returns one or more style properties for selected elements | |
Checks if any of the selected elements have a specified class | |
Sets or returns the height of selected elements | |
Sets or returns the position (relative to the document) for selected elements | |
Returns the first parent element that is positioned | |
Returns the position (relative to the parent element) of the first selected element | |
Removes one or more classes from selected elements | |
Sets or returns the horizontal position of the scrollbar for the selected elements | |
Sets or returns the vertical position of the scrollbar for the selected elements | |
Toggles between adding/removing one or more classes from selected elements | |
Sets or returns the width of selected elements |
jQuery AJAX
jQuery has a rich library of methods (functions) for AJAX development.
jQuery AJAX Example
|
What is AJAX?
AJAX = Asynchronous JavaScript and XML.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
AJAX is a technique for creating fast and dynamic web pages.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.
AJAX and jQuery
jQuery provides a rich set of methods for AJAX web development.
With jQuery AJAX, you can request TXT, HTML, XML or JSON data from a remote server using both HTTP Get and HTTP Post.
And you can load remote data directly into selected HTML elements of your web page!
Write Less, Do More
The jQuery load() method is a simple (but very powerful) AJAX function. It has the following syntax:
$(selector).load(url,data,callback)
Use the selector to define the HTML element(s) to change, and the url parameter to specify a web address for your data.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("div").load('test1.txt');
});
});
</script>
</head>
<body>
<div><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>
Only if you want to send data to the server, you need to use the data parameter. Only if you need to trigger a function after completion, you will use the callback parameter.
Low Level AJAX
$.ajax(options) is the syntax of the low level AJAX function.
$.ajax offers more functionality than higher level functions like load, get, and post, but it is also more difficult to use.
The option parameter takes name|value pairs defining url data, passwords, data types, filters, character sets, timeout and error functions.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({url:"test1.txt", success:function(result){
$("div").html(result);
}});
});});
</script>
</head>
<body>
<div><h2>Let AJAX change this text</h2></div>
<button>Change Content</button>
</body>
</html>
jQuery AJAX Methods From This Page:
Request | Description |
$(selector).load(url,data,callback) | Load remote data into selected elements |
$.ajax(options) | Load remote data into an XMLHttpRequest object |
jQuery AJAX Methods
AJAX is the art of exchanging data with a server, and update parts of a web page - without reloading the whole page.
The following table lists all the jQuery AJAX methods:
Method | Description |
Performs an AJAX request | |
Specifies a function to run when the AJAX request completes | |
Specifies a function to run when the AJAX request completes with an error | |
Specifies a function to run before the AJAX request is sent | |
Sets the default values for future AJAX requests | |
Specifies a function to run when the first AJAX request begins | |
Specifies a function to run when all AJAX requests have completed | |
Specifies a function to run an AJAX request completes successfully | |
Loads data from a server using an AJAX HTTP GET request | |
Loads JSON-encoded data from a server using a HTTP GET request | |
Loads (and executes) a JavaScript from the a server using an AJAX HTTP GET request | |
Loads data from a server and puts the returned HTML into the selected element | |
Creates a serialized representation of an array or object (can be used as URL query string for AJAX requests) | |
Loads data from a server using an AJAX HTTP POST request | |
Encodes a set of form elements as a string for submission | |
Encodes a set of form elements as an array of names and values |
No comments:
Post a Comment