J Query Tutorial
J Query Tutorial
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
</body>
</html>
What is jQuery?
jQuery is a lightweight, "write less, do more", JavaScript library.
jQuery takes a lot of common tasks that require many lines of JavaScript code
to accomplish, and wraps them into methods that you can call with a single
line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like
AJAX calls and DOM manipulation.
Tip: In addition, jQuery has plugins for almost any task out there.
Why jQuery?
There are lots of other JavaScript libraries out there, but jQuery is probably
the most popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery, such as:
● Google
● Microsoft
● IBM
● Netflix
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(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>
jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing
some action on the element(s).
$(“p”).hide;
Function functionname ()
Function sum()
Examples:
jQuery Selectors
jQuery selectors are one of the most important parts of the jQuery library.
jQuery selectors are used to "find" (or select) HTML elements based on their
name, id, classes, types, attributes, values of attributes and much more. It's
based on the existing CSS Selectors, and in addition, it has some own custom
selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
The element Selector
The jQuery element selector selects elements based on the element name.
$("p")
Example
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(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>
</body>
</html>
An id should be unique within a page, so you should use the #id selector
when you want to find a single, unique element.
To find an element with a specific id, write a hash character, followed by the
id of the HTML element:
$("#test")
Example:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(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>
Examples:
The term "fires/fired" is often used with events. Example: "The keypress
event is fired, the moment you press a key".
jQuery Syntax For Event Methods
In jQuery, most DOM events have an equivalent jQuery method.
$("p").click();
$("p").click(function(){
// action goes here!!
});
click()
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide
the current <p> element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
dblclick()
The function is executed when the user double-clicks on the HTML element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("p").dblclick(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
mouseenter()
The function is executed when the mouse pointer enters the HTML element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
});
</script>
</head>
<body>
</body>
</html>
mouseleave()
The function is executed when the mouse pointer leaves the HTML element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
</body>
</html>
mousedown()
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
});
</script>
</head>
<body>
</body>
</html>
mouseup()
The function is executed, when the left, middle or right mouse button is
released, while the mouse is over the HTML element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
});
</script>
</head>
<body>
</body>
</html>
hover()
The first function is executed when the mouse enters the HTML element, and
the second function is executed when the mouse leaves the HTML element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
</body>
</html>
focus()
</body>
</html>
blur()
/******* To Check********/
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color", "yellow");
});
$("input").blur(function(){
$(this).css("background-color", "green");
});
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("p").on("click", function(){
$(this).hide();
});
});
</script>
</head>
<body>
</body>
</html>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
The optional speed parameter specifies the speed of the hiding/showing, and
can take the following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
});
});
</script>
</head>
<body>
<button>Hide</button>
</body>
</html>
jQuery toggle()
You can also toggle between hiding and showing an element with the
toggle() method.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
</body>
</html>
Syntax:
$(selector).fadeIn(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<body>
<div id="div1"
style="width:80px;height:80px;display:none;background-
color:red;"></div><br>
<div id="div2"
style="width:80px;height:80px;display:none;background-
color:green;"></div><br>
<div id="div3"
style="width:80px;height:80px;display:none;background-
color:blue;"></div>
</body>
</html>
Syntax:
$(selector).fadeOut(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate fadeOut() with different parameters.</p>
</body>
</html>
● slideDown()
● slideUp()
● slideToggle()
Syntax:
$(selector).slideDown(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
</body>
</html>
$(selector).animate({params},speed,callback);
The optional speed parameter specifies the duration of the effect. It can take
the following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '250px'});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<div
style="background:#98bf21;height:100px;width:100px;position:absol
ute;"></div>
</body>
</html>
The stop() method works for all jQuery effect functions, including sliding,
fading and custom animations.
Syntax:
$(selector).stop(stopAll,goToEnd);
The optional stopAll parameter specifies whether also the animation queue
should be cleared or not. Default is false, which means that only the active
animation will be stopped, allowing any queued animations to be performed
afterwards.
So, by default, the stop() method kills the current animation being
performed on the selected element.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(5000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
font-size: 18px;
text-align: center;
background-color: #555;
color: white;
border: solid 1px #666;
border-radius: 3px;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
</body>
</html>
Examples
The example below has a callback parameter that is a function that will be
executed after the hide effect is completed:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide("slow", function(){
alert("The paragraph is now hidden");
});
});
});
</script>
</head>
<body>
<button>Hide</button>
<p>This is a paragraph with little content.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide(1000);
alert("The paragraph is now hidden");
});
});
</script>
</head>
<body>
<button>Hide</button>
</body>
</html>
Tip: This way, browsers do not have to find the same element(s) more than
once.
To chain an action, you simply append the action to the previous action.
The following example chains together the css(), slideUp(), and
slideDown() methods. The "p1" element first changes to red, then it slides
up, and then it slides down:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red").slideUp(2000).slideDown(2000);
});
});
</script>
</head>
<body>
<button>Click me</button>
</body>
</html>
Another example:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#p1").css("color", "red")
.slideUp(2000)
.slideDown(2000);
});
});
</script>
</head>
<body>
<button>Click me</button>
</body>
</html>
jQuery comes with a bunch of DOM related methods that make it easy to
access and manipulate elements and attributes.
The DOM defines a standard for accessing HTML and XML documents:
The following example demonstrates how to get content with the jQuery
text() and html() methods:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " + $("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " + $("#test").html());
});
});
</script>
</head>
<body>
</body>
</html>
The following example demonstrates how to get the value of an input field
with the jQuery val() method:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Value: " + $("#test").val());
});
});
</script>
</head>
<body>
<button>Show Value</button>
</body>
</html>
The following example demonstrates how to get the value of the href
attribute in a link:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("#ws").attr("href"));
});
});
</script>
</head>
<body>
</body>
</html>
The following example demonstrates how to set content with the jQuery
text(), html(), and val() methods:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text(function(i, origText){
return "Old text: " + origText + " New text: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i, origText){
return "Old html: " + origText + " New html: Hello <b>world!</b> (index: " + i + ")";
});
});
});
</script>
</head>
<body>
</body>
</html>
The following example demonstrates how to change (set) the value of the
href attribute in a link:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(document).ready(function(){
$("button").click(function(){
$("#ws").attr("href", "[Link]
});
});
</script>
</head>
<body>
<p>Mouse over the link (or click on it) to see that the value of the href attribute has
changed.</p>
</body>
</html>
The attr() method also allows you to set multiple attributes at the same
time.
The following example demonstrates how to set both the href and title
attributes at the same time:
<!DOCTYPE html>
<html>
<head>
<script src="[Link]
<script>
$(document).ready(function(){
$("button").click(function(){
$("#ws").attr({
"href" : "[Link]
"title" : "xyztutorial"
});
});
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append(" <b>Appended text</b>.");
});
$("#btn2").click(function(){
$("ol").append("<li>Appended item</li>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").prepend("<b>Prepended text</b>. ");
});
$("#btn2").click(function(){
$("ol").prepend("<li>Prepended item</li>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
</body>
</html>
However, both the append() and prepend() methods can take an infinite
number of new elements as parameters. The new elements can be generated
with text/HTML (like we have done in the examples above), with jQuery, or
with JavaScript code and DOM elements.
In the following example, we create several new elements. The elements are
created with text/HTML, jQuery, and JavaScript/DOM. Then we append the
new elements to the text with the append() method (this would have worked
for prepend() too) :
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
function appendText() {
var txt1 = "<p>Text.</p>"; // Create text with HTML
var txt2 = $("<p></p>").text("Text."); // Create text with jQuery
var txt3 = [Link]("p");
[Link] = "Text."; // Create text with DOM
$("body").append(txt1, txt2, txt3); // Append new elements
}
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button onclick="appendText()">Append text</button>
</body>
</html>
The jQuery before() method inserts content BEFORE the selected HTML
elements.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("img").before("<b>Before</b>");
});
$("#btn2").click(function(){
$("img").after("<i>After</i>");
});
});
</script>
</head>
<body>
In the following example, we create several new elements. The elements are
created with text/HTML, jQuery, and JavaScript/DOM. Then we insert the new
elements to the text with the after() method (this would have worked for
before() too) :
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
function afterText() {
var txt1 = "<b>I </b>"; // Create element with HTML
var txt2 = $("<i></i>").text("love "); // Create with jQuery
var txt3 = [Link]("b"); // Create with DOM
[Link] = "jQuery!";
$("img").after(txt1, txt2, txt3); // Insert new elements after img
}
</script>
</head>
<body>
</body>
</html>
Remove Elements/Content
To remove elements and content, there are mainly two jQuery methods:
////******To check
<!DOCTYPE html>
<html>
<head>
<script
src=”[Link]
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").remove();
});
});
</script>
</head>
<body>
</div>
<br>
<button>Remove div element</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").empty();
});
});
</script>
</head>
<body>
</div>
<br>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").remove(".test");
});
});
</script>
<style>
.test {
color: red;
font-size: 20px;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<p class="test">This is another paragraph.</p>
<p class="test">This is another paragraph.</p>
</body>
</html
>
What is AJAX?
AJAX = Asynchronous JavaScript and XML.
In short; AJAX is about loading data in the background and display it on the
webpage, without reloading the whole page.
With the jQuery AJAX methods, you can request text, HTML, XML, or JSON
from a remote server using both HTTP Get and HTTP Post - And you can load
the external data directly into the selected HTML elements of your web page!
The load() method loads data from a server and puts the returned data into
the selected element.
Syntax:
$(selector).load(URL,data,callback);
The required URL parameter specifies the URL you wish to load.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
</body>
</html>
The following example loads the content of the element with id="p1", inside
the file "demo_test.txt", into a specific <div> element:
The optional callback parameter specifies a callback function to run when the
load() method is completed. The callback function can have different
parameters:
The following example displays an alert box after the load() method
completes. If the load() method has succeeded, it displays "External
content loaded successfully!", and if it fails it displays an error message:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + [Link] + ": " + [Link]);
});
});
});
</script>
</head>
<body>
</body>
</html>
The jQuery get() and post() methods are used to request data from the
server with an HTTP GET or POST request.
POST can also be used to get some data from the server. However, the POST
method NEVER caches data, and is often used to send data along with the
request.
The required URL parameter specifies the URL you wish to request.
The following example uses the $.get() method to retrieve data from a file
on the server:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
</body>
</html>
jQuery $.post() Method
The $.post() method requests data from the server using an HTTP POST
request.
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
</body>
</html>
Then we pass in some data to send along with the request (name and city).
<%
dim fname,city
fname=[Link]("name")
city=[Link]("city")
[Link]("Dear " & fname & ". ")
[Link]("Hope you live well in " & city & ".")
%>
jQuery Filters
Use jQuery to filter/search for specific elements.
Filter Tables
Perform a case-insensitive search for items in a table:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myTable tr").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value)
> -1)
});
});
});
</script>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<h2>Filterable Table</h2>
<p>Type something in the input field to search the table for
first names, last names or emails:</p>
<input id="myInput" type="text" placeholder="Search..">
<br><br>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody id="myTable">
<tr>
<td>John</td>
<td>Doe</td>
<td>john@[Link]</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary@[Link]</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july@[Link]</td>
</tr>
<tr>
<td>Anja</td>
<td>Ravendale</td>
<td>a_r@[Link]</td>
</tr>
</tbody>
</table>
</body>
</html>
Filter Lists
Perform a case-insensitive search for items in a list:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myList li").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value)
> -1)
});
});
});
</script>
</head>
<body>
<h2>Filterable List</h2>
<p>Type something in the input field to search the list for
specific items:</p>
<input id="myInput" type="text" placeholder="Search..">
<br>
<ul id="myList">
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth</li>
</ul>
</body>
</html>
Filter Anything
Perform a case-insensitive search for text inside a div element:
<!DOCTYPE html>
<html>
<head>
<script
src="[Link]
[Link]"></script>
<script>
$(document).ready(function(){
$("#myInput").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#myDIV *").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value)
> -1)
});
});
});
</script>
</head>
<body>
<h2>Filter Anything</h2>
<p>Type something in the input field to search for a specific
text inside the div element with id="myDIV":</p>
<input id="myInput" type="text" placeholder="Search..">
<div id="myDIV">
<p>I am a paragraph.</p>
<div>I am a div element inside div.</div>
<button>I am a button</button>
<button>Another button</button>
<p>Another paragraph.</p>
</div>
</body>
</html>