Monday, July 21, 2008

jQuery

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…

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

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

   1: $("#menu li div.subMenuItems").hide();            
   2: $("#menu li div.parentMenuItem").click(function()
   3: {            
   4:     $("#menu li div.subMenuItems:visible").slideUp("slow");                
   5:     $(this).next().slideDown("slow");
   6:     return false;
   7: });

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.

No comments:

Post a Comment