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:

 $('#ID_Goes_Here') 

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

 $('.CSS_Class_Name_Goes_Here') 

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(this.id); }); 

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.

 $("#Panel1").html("
  • 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
	

	



	





Links
jQuery: http://www.jQuery.com/ent Object Model: http://en.wikipedhttp://en.wikipedia.org/wiki/Document_Object_Model

All good things come to an end…

but they never say much about bad things and beginnings. I guess we’ll just have to see where this ends up. I figure if this thing never ends, then it’s pretty obvious what this is.

It’s also going to be obvious as to what this is not. This is not a place for incredible solutions to programming problems. This is not written by an expert. I don’t work for some kind of think tank. I haven’t been programming since I was 2. Simply put, I am a moderate programmer that for some reason has decided to take programming seriously and figure out all I can about .net 3.5 and beyond… without having much time with 2.0. In other words, FUN.

I can’t swear that everything I put in here is 100% accurate. It should build, but my explanations could be a bit off. Well let me put that in another way… they are off in that they most likely will not make sense the first time around. They also could be somewhat wrong. Either way, for those 2 people reading this, please leave a comment.