J Query Modified Tutorials
J Query Modified Tutorials
J Query Modified Tutorials
JQuery Selectors
Basic jQuery Selectors
For examples in this tutorial we will write code inside html page. Inside HTML we will have
<script></script> block where we will put HTML code.
We’ll write all jQuery selectors code inside document.ready() function as follows-
<script type="text/javascript">
$("document").ready(function() {
//Selectors code goes here
});
</script>
<style type="text/css">
Examples-
<script type="text/javascript">
$("document").ready(function() {
//select all paragraphs of the document
$("p").css("border", "3px solid red");
//Select all list items those are inside the unordered list
//and also has class a
$("ul li.a").css("border", "3px solid red");
});
</script>
Basic jQuery Filter Selectors
<script type="text/javascript">
$("document").ready(function() {
//create border of the first paragraph
$("p:first").css("border","3px solid red");
});
</script>
Basic jQuery Attribute Selectors
Example
/* Select paragraph that has id Attribute value value starts with para and
also has contains the value of lang attribute = en-
note during placing multiple attributes in following way no space should
allowed. if space given no outcome should seen in brower*/
$("p[id^=para][lang*=en-]").css("border","3px solid red");
Content & Visibility Filters
//find the paragraph tag that contains text 3
//find the ul that has a li and the li has class attribute with name a
$("ul:has(li[class=a])").css("border","3px solid red");
//find the li inside ul that has nth child = 3 i.e. 3rd child
$("ul li:nth-child(3)").css("border","3px solid red");
//select the last li of ul i.e. select the last child (li) of parent (ul)
$("ul li:last-child").css("border","3px solid red");
$("document").ready(function() {
alert($('li')).get(0);
var leftmargin = 0;
var border = 3;
$("p").each(function(){
$(this).css("border", border+"px solid red");
$(this).css("margin-left",leftmargin);
border+=2;
leftmargin+=10;
})
});
jQuery Statement Chaining
//When click on any link (<a> tag) of the page it will open in the new
window
//In our image case- when we will click on the image it (the image) will
open a new window
$("a").attr("target", "_blank");
//we remove href attribute from link. When we need to turn link on or off
we need it
$("a").removeAttr("href");
Complete Example-
AutoTOC_finished.html
3. Working with jQuery Events
In jQuery we will write event handling code inside jQuery function that we will declare as follows-
<script type="text/javascript">
$(function() {
//Write event binding and event handling code here
});
});
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Binding and Unbinding Events with jQuery</title>
<script type="text/javascript" src="../jquery-1.3.2.js"></script>
<script type="text/javascript">
$(function() {
//bind the evtTarget div with moseover & mouseleave event with
//highlight function
$("#evtTarget").bind("mouseover mouseleave", highlight);
//highlight function.
//Note: eventhandler function in jQuery always take an event object e.g.-evt as parameter
//Thsi is (parameter taking) applicable even we're not going use them
function highlight(evt) {
//refer the eventTarget div
//also called toggleClass function and
//it will accept highlighted call as argument of the function
$("#evtTarget").toggleClass("highlighted");
}
</script>
<style type='text/css'>
.normal {
width:300px;
height:200px;
background-color:Yellow;
font-size:18pt;
}
/*highlighted class*/
.highlighted {
background-color:Red;
}
</style>
</head>
<body>
<h1>Binding Event Example</h1>
<div id="evtTarget" class="normal">Mouse over this div to see the effect. Click on it to unbind the
mouseover.</div>
</body>
</html>
Output: Before mouse hover-
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Binding and Unbinding Events with jQuery</title>
<script type="text/javascript" src="../jquery-1.3.2.js"></script>
<script type="text/javascript">
$(function() {
//called hover function and passed
//highlight function for both parameters of hover function
//This behave same as mouseenter and mouseleave event as
//we seen in previous example
$("#evtTarget").hover(highlight, highlight);
function highlight(evt) {
$("#evtTarget").toggleClass("highlighted");
}
//function for click1 where html will show Click!
function fnClick1() {
$("#evtTarget").html("Click!");
}
//function for click2 where html will show Clack! instead of click
function fnClick2() {
$("#evtTarget").html("Clack!");
}
</script>
<style type='text/css'>
.normal {
width:300px;
height:200px;
background-color:Yellow;
font-size:18pt;
}
.highlighted {
background-color:Red;
}
</style>
</head>
<body>
<h1>Using Event Helpers</h1>
<div id="evtTarget" class="normal">Mouse over this div to see the effect. Click on it to change the
content.</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Using the jQuery Event Object</title>
<script type="text/javascript" src="../jquery-1.3.2.js"></script>
<script type="text/javascript">
$(function() {
//Write an event that will set some information in all div
//so when click on those divs it will show those informations
$("div").click(function(evt) {
$(this).html("pageX: " + evt.pageX + ", pageY: "
+ evt.pageY + ", type: " + evt.type + ", target: " +
evt.target);
});
});
</script>
<style type='text/css'>
.normal {
width:300px;
height:200px;
background-color: Silver;
font-size:18pt;
margin:5pt 5pt 5pt 5pt;
}
</style>
</head>
<body>
<h1>Using the jQuery Event Object</h1>
<div id="Div1" class="normal">Click on this div (Div1) to see the event information</div>
<div id="Div2" class="normal">Click on this div (Div2) to see the event information</div>
<div id="Div3" class="normal">Click on this div (Div3) to see the event information</div>
</body>
</html>
Example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Striping/Hover Highlighting a Table</title>
<script type="text/javascript" src="../jquery-1.3.2.js"></script>
<script type="text/javascript">
$(function() {
$("#theList tr:even").addClass("stripe1");
$("#theList tr:odd").addClass("stripe2");
$("#theList tr").hover(
function() {
$(this).toggleClass("highlight");
},
function() {
$(this).toggleClass("highlight");
}
);
});
</script>
<style type="text/css">
th,td {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 18px;
color: #000000;
}
tr {
border: 1px solid gray;
}
td {
width:200px;
padding:3px;
}
th {
background-color:#D2E0E8;
color:#003366
}
table {
border: 1pt solid gray;
}
.clickable {
cursor:pointer;
}
.stripe1 {
background-color:#0f0;
}
.stripe2 {
background-color:#afa;
}
.highlight {
background-color: #ffcc00;
font-weight:bold;
}
</style>
</head>
<body>
<h1>Using jQuery to stripe and hover-highlight a table</h1>
<table id="theList">
<thead>
<tr>
<th>Item</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Milk</td>
<td>1.99</td>
</tr>
<tr>
<td>Eggs</td>
<td>2.29</td>
</tr>
<tr>
<td>Butter</td>
<td>3.49</td>
</tr>
<tr>
<td>Bread</td>
<td>0.99</td>
</tr>
<tr>
<td>Pasta</td>
<td>1.19</td>
</tr>
<tr>
<td>Honey</td>
<td>4.39</td>
</tr>
<tr>
<td>Cookies</td>
<td>2.99</td>
</tr>
<tr>
<td>Apples</td>
<td>0.59</td>
</tr>
<tr>
<td>Sugar</td>
<td>1.78</td>
</tr>
<tr>
<td>Pepper</td>
<td>1.56</td>
</tr>
</tbody>
</table>
</body>
</html>