Lecture 2
Lecture 2
c o m )
jQuery Selectors
LECTURE (2)
The jQuery element selector selects elements based on the element name.
Example
<!DOCTYPE html>
<html>
<head>
<title>Element selector</title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
$("h1").hide('slow');
$("img").hide(4000);
});
});
</script>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<img src="pic/abc.jpg" width="300px" height="300px" /><br>
<button>Click me to hide paragraphs</button>
</body>
</html>
1 | P a g e If you have any query joint the facebook group also for help and online lectures and tutorials.
https://www.facebook.com/groups/pofwccteachersofficial
Lecture 2<jQuery Selectors> Prepared by Mr. Noman Tanseer (N o m a n s h 3 9 @ g m a i l . c o m )
What is id selector?
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.
An id should be unique within a page, so you should use the #id selector when you want to find a single,
unique element.
Example
<!DOCTYPE html>
<html>
<head>
<title>ID selector</title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
$("button").click(function(){
$("#hone").fadeOut(2000);
$("#htwo").fadeOut(3000);
$("#hthree").fadeOut(4000);
$("#pone").fadeOut();
$("#ptwo").fadeOut('slow');
});
});
</script>
</head>
<body>
<h1 id="hone">This is a heading</h1>
<h1 id="htwo">This is a heading</h1>
<h1 id="hthree">This is a heading</h1>
<p id="pone">This is a paragraph.</p>
<p id="ptwo">This is another paragraph.</p>
<button>Click me to fadeOut</button>
</body>
</html>
First selector is used to select the first element while last selector is used to select the last element.
Example
<!DOCTYPE html>
<html>
<head>
<title>First and Last selector</title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
$("li:first").addClass('red');
$("li:last").addClass('blue');
});
</script>
3 | P a g e If you have any query joint the facebook group also for help and online lectures and tutorials.
https://www.facebook.com/groups/pofwccteachersofficial
Lecture 2<jQuery Selectors> Prepared by Mr. Noman Tanseer (N o m a n s h 3 9 @ g m a i l . c o m )
<style type="text/css">
.red{
background-color:red;
border:5px groove #cccccc;
width:500px;
}
.blue{
background-color:blue;
border:5px groove #cccfff;
width:500px;
}
</style>
</head>
<body>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
</body>
</html>
Even selector is used to select the even elements while odd selector is used to select the odd elements.
Example
<!DOCTYPE html>
<html>
<head>
<title>Even and Odd Selector</title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/Javascript">
$(document).ready(function(){
$("li:even").addClass('red');
$("li:odd").addClass('blue');
});
</script>
<style type="text/css">
4 | P a g e If you have any query joint the facebook group also for help and online lectures and tutorials.
https://www.facebook.com/groups/pofwccteachersofficial
Lecture 2<jQuery Selectors> Prepared by Mr. Noman Tanseer (N o m a n s h 3 9 @ g m a i l . c o m )
.red{
background-color:red;
border:5px groove #cccccc;
width:500px;
}
.blue{
background-color:blue;
border:5px groove #cccfff;
width:500px;
}
</style>
</head>
<body>
<ul>
<li>First item</li>
<li>Second item</li>
<li>Third item</li>
<li>Fourth item</li>
</ul>
</body>
</html>
First-child is used to select first element of any tag while last-child is used to select last element of any tag.
Example
<!DOCTYPE html>
<html>
<head>
<title>Fist-child & Last-child Selector</title>
<script src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//$("ul li:first-child").hide(3000);
$("ul li:last-child").hide(3000);
});
</script>
</head>
<body>
<p>List 1:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
7 | P a g e If you have any query joint the facebook group also for help and online lectures and tutorials.
https://www.facebook.com/groups/pofwccteachersofficial
Lecture 2<jQuery Selectors> Prepared by Mr. Noman Tanseer (N o m a n s h 3 9 @ g m a i l . c o m )
<p>List 2:</p>
<ul>
<li>Coffee</li>
<li>Milk</li>
<li>Tea</li>
</ul>
</body>
</html>
8 | P a g e If you have any query joint the facebook group also for help and online lectures and tutorials.
https://www.facebook.com/groups/pofwccteachersofficial