My jQuery Primer

So you have been reading about jQuery and want to dive in and try some? I recently attended the MSDN Dev Conference in Detroit where jQuery integration and client-side programming were very hot topics. With Microsoft’s acceptance of the open source library, I figured I would give it a try. This is what I have learned so far. Before I can show you what you can do with jQuery, I think I should probably show you how to get a reference to jQuery into your code. In ASP.NET you can add your reference directly to your Script Manager


What does jQuery have to offer? First and foremost, jQuery has given me power over the Document Object Model (DOM)! jQuery Selectors are the greatest thing since sliced bread, no lie. Being able to EASILY find an object or group of objects in the DOM by ID, CSS Class, or by HTML Tag is something web developers have long needed. To select an object from the DOM by ID would look like this:


To select an object from the DOM by CSS Class would look like this:


To select an object from the DOM by HTML Tag would look like this:


With jQuery Selectors being so simple, it allows the developer to easily select an object or a group of objects. Now that we can select objects, what can we do with them? This is where the power of Selectors really builds into what else you can do. In a web application, round trips to the server to manage UI is wasteful. I avoid JavaScript like the plague because it’s a pain in the ass. jQuery makes client-side UI management feel like climbing the rope in gym class. For example, if I have a label that I want to be rendered but not visible I could create the label.


And later on in jQuery I can hide it like this.

 $('#Label4').css('display', 'none'); 

It’s nice to be able to easily select an object and modify it, but what if you have a whole group of items you want to modify? With jQuery, you can EASILY loop through a collection of objects. In this example I have a group of labels.


Now I want to update the text of each label to include its ID. I am going to loop through each object in the DOM with a CSS Class of .myLabel.

 $('.myLabel').each(function() { $(this).append(; }); 

What the jQuery code above does is execute a function that will append the object’s ID to its text value. Notice the Selector inside the function $(this). The syntax is used to find the loop’s current object in the DOM. I do a lot of web work where I create controls on the fly. For my last example, I just wanted to show a way to quickly insert some HTML into a container object. I will start with a Panel AKA a DIV.


Now I am going to use jQuery to select my DIV and add some HTML to it.

  • Item One
  • Item 2

Now I have shown you some snippets here and there, let me show you what my page actually looks like.


	jQuery Examples



jQuery: Object Model: http://en.wikiped

21 thoughts on “My jQuery Primer”

  1. Sean,

    Welcome to the club. I have been working with jQuery for about a month now and I am a convert. It is infectuous.

    I now have to have a very good reason to use an server control instead of an HTML tag.

    Another benefit is that the use of the selectors has really broadened my understanding and use of CSS.


  2. There is an error in your first label example. You use: $(‘#Label4’).css(‘display’, ‘none’);, but the ID of the label will change into “ASP.Net” ID, when the page is rendered.

    1. @Kenneth The ID should only be different if the control is contained within another .NET object. This is something that I have heard is going to change in ASP.NET 4.0. The ID you set will be the client ID. You can build the jQuery code dynamically and use Control.ClientID

  3. This is great, but I have worked with JQuery in ASP.NET, and $(‘#Panel1’), or any of the other server control id’s in there won’t work, as renders the controls with different id’s. The syntax would rather be $(‘#’)

  4. I’s reading some other jQuery starter articles. Happy to see this one too.
    Really it had helped me.

  5. Hi,

    Nice post. But unfortunately you cannot always access ASP.NET controls in that simple way in jQuery.

    You have to use: $(“#<%=yourControl.ClientID”)

Comments are closed.