In last article, we had looked into JQuery's history and benefits. In this article, we will look into the selectors available for accessing elements of DOM. Before that, get a copy of JQuery 1.4 minified version from http://jquery.com/ . Now, add downloaded jquery-1.4.2.min.js file to below html file named as JQuerySelectors.html.
<html>
<head>
<title>JQuery Sample HTML Page</title>
<script src="jquery-1.4.2.min.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#div1').hide();
});
</script>
</head>
<body>
<div>
<div id="div1">This is a Section.</div>
<div>This is next Section.</div>
<div>This is Last Section.</div>
</div>
</body>
</html>
First, we are including JQuery file using script tag. Then, we wrote custom script to hide div1. We always need to place JQuery script tag first before any custom scripts, to make JQuery framework available for the document.
The basic operation in JQuery is selecting an element in DOM. This is done with the help of $() construct with a string parameter containing any CSS selector expression. $() will return zero or more DOM elements on which we can apply a effect or style.
$(document).ready() indicates that code in it need to executed once the DOM got loaded. It won't wait for the images to load for executing the JQuery script. We created an anonymous function inside ready() function to hide div1.
We can rewrite $(document).ready() as jQuery (document).ready(), since $ is an alias for jQuery. Always use jQuery in place of $, if you are using more than one JavaScript library to resolve conflicts with JQuery library. The methods called on $(), will implicitly applied on all the elements returned by it without need of explicit looping. Let's say, $('.myclass').hide() will hide all elements with class as myclass with implicit looping.
As we discussed earlier, $() accepts a string parameter having tag name [like div, p] or Element Id or class name as shown in below table:
|
Selector |
JQuery Syntax |
Description |
|
Tag Name |
$('div') |
All div tags in the document |
|
ID |
$('#TextId') |
Selects element with ID as TextId. It starts with # followed Element Id. |
|
Class |
$('.myclass') |
Selects all elements with class as myclass. It starts with '.' followed by class name |
For complete list of selector's syntax, refer http://www.w3.org/TR/css3-selectors/ or refer below table:

Ajeet MishraPosted Jan 22, 2016, 2:37 AM
Nice one
Vipul MalhotraPosted Nov 4, 2015, 9:07 AM
nice article