Jquery Introduction: Previous Next Chapter
Jquery Introduction: Previous Next Chapter
The jQuery library can be added to a web page with a single line of
markup.
Before you start studying jQuery, you should have a basic knowledge of:
HTML
CSS
JavaScript
If you want to study these subjects first, find the tutorials on our Home
page.
What is jQuery?
The jQuery library is stored a single JavaScript file, containing all the
jQuery methods.
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
Please note that the <script> tag should be inside the page's <head>
section.
Example
<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>
Try it yourself »
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.
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></
script>
</head>
Try it yourself »
Microsoft
<head>
<script type="text/javascript"
src="http://ajax.microsoft.com/ajax/jquery/jquery-
1.4.2.min.js"></script>
</head>
jQuery Syntax
With jQuery you select (query) HTML elements and perform "actions"
on them.
$(this).hide()
Demonstrates the jQuery hide() method, hiding the current HTML
element.
$("#test").hide()
Demonstrates the jQuery hide() method, hiding the element with
id="test".
$("p").hide()
Demonstrates the jQuery hide() method, hiding all <p> elements.
$(".test").hide()
Demonstrates the jQuery hide() method, hiding all elements with
class="test".
jQuery Syntax
The jQuery syntax is tailor made for selecting HTML elements and
perform some action on the element(s).
Examples:
You might have noticed that all jQuery methods, in our examples, are
inside a document.ready() function:
$(document).ready(function(){
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:
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.
Example
$("p").css("background-color","yellow");
Try it yourself »
Syntax Description
$(this) Current HTML element
$("p") All <p> elements
$("p.intro") All <p> elements with class="intro"
$(".intro") All elements with class="intro"
$("#intro") The first element with id="intro"
$("ul li:first") The first <li> element of each <ul>
All elements with an href attribute that ends with
$("[href$='.jpg']")
".jpg"
$("div#intro All elements with class="head" inside a <div>
.head") element with id="intro"
Example
<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>
Try it yourself »
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();
Example
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_jquery_functions.js"></script>
</head>
jQuery Events
Here are some examples of event methods in jQuery: