9 November 2009 2 Comments

jQuery Optimization : Avoid Unnecessary Styling

This is another common mistake that many jQuery developer made. jQuery provides us with the ability to manipulate styling through styling methods. Although it is very convenient to do that but it also contribute to the size of the script and makes maintenance difficult. Furthermore, some of the selector is made solely for styling purposes which makes it even undesirable. To make thing worst, the styles made with jQuery are all inline style which doesn’t help caching. Hence, instead of styling everything with jQuery. It is much more advisable to give classes to your tag and style it on a external stylesheet. Instead of writing jQuery styling such as these,

jQuery('div').each(function(){
	var obj = jQuery(this);
	obj.css({'background-color' : 'yellow', 'font-weight' : 'bolder', 'color': '#000', 'font-size': '15px'})
});

Where styling is being applied through jQuery, we can just add a class or even better do it on the HTML file itself!

jQuery('div').each(function(){
	var obj = jQuery(this);
	obj.addClass("mystyling");
});

This way we can easily eliminate a lot of extra code in our script. Unless, absolutely necessary to manipulate styling through jQuery, it is better to leave styling to the stylesheet.

http://jqueryblog.net/wp-content/plugins/sociofluid/images/digg_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/reddit_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/dzone_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/stumbleupon_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/delicious_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/blinklist_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/blogmarks_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/furl_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/newsvine_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/technorati_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/magnolia_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/google_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/myspace_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/facebook_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/yahoobuzz_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/sphinn_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/mixx_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/twitter_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/jamespot_24.png http://jqueryblog.net/wp-content/plugins/sociofluid/images/meneame_24.png

2 Responses to “jQuery Optimization : Avoid Unnecessary Styling”

  1. CoryMathews 10 November 2009 at 3:21 pm #

    I agree. I really hate when developers put the style in the script mainly because they then proceed to only give you the compact version of the script. Thus to change the look becomes much more difficult. So using the classes also makes styling the code much easier for future changes.

  2. Christopher McKenzie 25 November 2009 at 1:45 am #

    I found this post to be very helpful!


Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes