jQuery
1 min read

I have recently started using jQuery and I’ve been quite impressed. I found integrating jQuery most useful while I developed my first ASP.NET MVC Preview 3 application.

jQuery was able to simplify some of the things I would have had to write nasty looking View code otherwise.

For example, I wanted to alternate every other row in a table with a different color. To do this in the View I initially created a counter variable and did a mod 2 to apply the CSS class. Once I integrated jQuery I was able to remove that messy code throughout my entire View and just replace it with…

<script type="text/javascript">
     $(document).ready(function(){
         $("#tblList tr:odd").addClass("alternatingRow");
         $("#tblList tr:even").addClass("row");
     });
 </script>

After I got a little familiar with jQuery I branched out and used it to create an accordion navigation menu with little effort.

 $("#menu li div.subMenuItems").hide();
 $("#menu li div.parentMenuItem").click(function()
 {
     $("#menu li div.subMenuItems:visible").slideUp("slow");
     $(this).next().slideDown("slow");
     return false;
 });

There are several other helpful areas in my ASP.NET MVC application that I found jQuery to be very convenient, efficient, and resulted in a cleaner code base.

If you haven’t used jQuery before I highly recommend you check it out.

Edit post on GitHub


Design Pattern Riddle #7
ASP.NET AJAX 4.0 CodePlex Preview 1