SIGN UP MEMBER LOGIN:    
ARTICLE

JQuery and CSS Selectors: Part 1

Posted by Abhimanyu Kumar Vatsa Articles | JQuery February 09, 2012
In this article you are going to learn all about jQuery and CSS Selectors; if you are still not using jQuery in your web applications, then this article will motivate you.
Reader Level:

Introduction

This is my very first article on any jQuery specific title; I have posted many articles using jQuery with ASP.NET within the past few days. Now I'm wishing to explore jQuery from the very basics; here comes my title 'jQuery CSS Selectors'. I'm not going to talk about definitions here, just going to show you something using examples. So, let's begin.

Look at this table:

Type of Selector

In CSS

In jQuery

Details

ID

#name { }

$('#name')

It will select all controls/elements that have id = 'name'.

Class

.address { }

$('.address)

It will select all controls/elements that have class = 'address'.

Using Tag Name

div { }

$('div')

It will select all div in the page.

In the above table you can see three commonly used selector types (using id, class or tag name) and how we call it in CSS and jQuery. Don't get confused here; look at the example to get it better.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Exploring jQuery</title>
    <style type="text/css">
        #name
        {
            background-color:Fuchsia;
        }
        .address
        {
            background-color:Lime;
        }
        div
        {
            background-color:Gray;
        }
    </style>
</head>
<
body>
    <!--Exaple on id selector-->
    <p id="name">
        I am in ID selector.
    </p>
    <!--Example on class selector-->
    <p class="address">
        I am in Class selector.
    </p>
    <!--Example on tag selector-->
    <div>
        I am in Tag selector.
    </div>
</body>
</
html>

Output screen:

image001.jpg

In above example, in the body I have two paragraphs and one div. The first paragraph is using an id selector, the second is using a class selector and th thired is the tag itself. In the head section you can see I am calling the first paragraph which has an id="name" using # selector type, the second paragraph has class="address" using . selector type and the last one directly uses the div tag.

Let's back to jQuery.

In jQuery we use an id selector type as $('#name'), class selector type as $('.address) and tag selector type as $('div').

Now, let's convert the above example from CSS to jQuery.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Exploring jQuery</title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#name').css('background-color', 'Fuchsia');
            $('.address').css('background-color', 'Lime');
            $('div').css('background-color', 'Gray');
        });
    </script>
</head>
<
body>
    <!--Exaple on id selector-->
    <p id="name">
        I am in ID selector.
    </p>
    <!--Example on class selector-->
    <p class="address">
        I am in Class selector.
    </p>
    <!--Example on tag selector-->
    <div>
        I am in Tag selector.
    </div>
</body>
</
html>

Output screen

image001.jpg

Wow!! The output is the same with jQuery but jQuery makes it quick and even more robust.

In the above example you can see I am referring to the jQuery library file using:

<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

And also using a jQuery method:

    <script type="text/javascript">
        $(document).ready(function () {
            $('#name').css('background-color', 'Fuchsia');
            $('.address').css('background-color', 'Lime');
            $('div').css('background-color', 'Gray');
        });
    </script>

In the next part you will see the actual example of robustness in jQuery.

I hope you like it. Thanks.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor