jQuery Tips and Tricks II

Written by Kevin Liew on 07 Apr 2009
115,902 Views • Techniques


This is my second posts about jQuery Tips and Tricks. If you missed my first post, you might want to read it now useful and handy jQuery Tips and Tricks. This time, I found some performance tuning, element manipulations tips and tricks that I believe will able to help you develop a more effective and efficient jQuery script. Enjoy!

1. No conflict-mode

To avoid conflict when you are using multiple libraries in a website, you can use this jQuery Method, and assign a different variable name instead of the dollar sign. Using jQuery with other libraries

var $j = jQuery.noConflict();

2. Total of selected elements

It return the number of elements that are selected by jQuery selector. Using length method will return the size of array which is equal to the number of selected objects.


3. Let Google host jQuery for you

There are advantages to let Google host jQuery. Some of us may not know, it actually provides better caching, increase parallelism and decreased latency compared with hosting it locally. Find out more about it: 3 reasons why you should let google host jQuery for you

<script src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.2.6");
google.setOnLoadCallback(function() {
    //Code to be inserted in this area

/* the best way (fastest and most efficient implementation) */
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    //This is more like it!

4. Check if checkbox is checked

jQuery provides us 3 ways to determine if a checkbox is checked. How to check if checkbox is checked

// First way 

// Second way 

// Third way 
    function() { 
       // Insert code here 

5. How to disabled/enable an element with jQuery


// To disable 
$('.someElement').attr('disabled', 'disabled'); 

// To enable 
// OR you can set attr to "" 
$('.someElement').attr('disabled', '');

6. Improving jQuery code performance

Selecting an element using ID is faster than selecting classname. jQuery Performance Analysis of selectors.

update: apparently, jQuery 1.3 has a big improvement on this. So, I guess this tips only suitable for old browsers and jQuery 1.2 or below. Thanks Karl for the information.

/* ID */

/* is faster than class*/

If you use a classname, the jQuery will perform better if you attached the element to the class name. A good practise when selecting a class:


/* is faster than this*/


7. Javascript Error Checker

Debugging javascript script can be extremely time consuming. So, if you running out of ideas what is causing the errors, you can use this free online tool JSLint

8. Disable jQuery animations

This will disable all jQuery animations if it's set to true.

jQuery.fx.off = true;

9. IE Rendering problem in fadeIn effect

If you used the fadeIn effect in ie6 before, you probably able to notice how urgly it looks. The text doesn't render the way it should. To solve this, you need to remove the filter attribute

// This causes this text glich

// This will fix it

10. Check if jQuery.js is loaded


/* Method 1 */
if (jQuery) {  
    // jQuery is loaded  
} else {
    // jQuery is not loaded

/* Method 2 */
if (typeof jQuery == 'undefined') {  
    // jQuery is not loaded  
} else {
    // jQuery is loaded

11. Add an icon for external links

This trick is pretty simple and good for usability. If links are linking to external website, an image will be displayed next to the link. Dynamically add an icon for external links

$(document).ready(function() {
  $('#extlinks a').filter(function() {
 return this.hostname && this.hostname !== location.hostname;
external link


I hope this list will able to help you. If you have some tips and tricks that you want to share with all of us. Please leave in comment. : ) Thanks!

How to Decrease Server Issues from Web Hosting Search

Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.

Alexander 15 years ago
Tip 9 doesn't work for me. In IE7 ist's the same ugly effect like before.
ZK@Web Marketing Blog 14 years ago
What you probably should look at doing is showing the first panel by default so it is still visible without Javascript and then rather than have the links href set to just '#' make them link to pages showing the information from each panel.
Rob @ Web Design Talk 14 years ago
Like the tip about the fadein rendering in IE - always wondered about that. I used to get that problme in IE7 aswell.

@Grunties: there are loads of reasons to left Google host your JQuery, with the main being the fact that you're in essence getting CDN for free.

Rob @ Web Design Talk 14 years ago
Like the tip about the fadein rendering in IE - always wondered about that. I used to get that problme in IE7 aswell.

@Grunties: there are loads of reasons to left Google host your JQuery, with the main being the fact that you're in essence getting CDN for free.
Shahriar Hyder 14 years ago
Excellent collection of jQuery Tips and Tricks mate. I have also linked to yours from my blog post below where I am trying to collect the most useful and common jQuery code snippets for JavaScript over the web. Here is the title and the link to the jQuery link compilation endeavor:

Ultimate collection of top jQuery tutorials, tips-tricks and techniques to improve performance

molokoloco 14 years ago
Some very good advices.. but always need some more ;)

That not well explained but i'm index and build some jQuery tips here http://code.google.com/p/molokoloco-coding-project/wiki/JavascriptJquery
Web hosting 14 years ago
Good comment this "I have to backup on my initial claim and apologize to Joe.
"Google hosting your jQuery is a 100% bad idea" - is just my strong opinion.
The fact is that the usefulness of Google hosting your jQuery is debatable. Much more broad statistical data is needed to establish it one way or the other.
My story is... in late 2008 I used Google to host jQuery for two websites, One (~600 pages) I am maintaining on a daily basis the other was actively being developed. In a day or so i noticed occasional slowdowns. It lasted for week or so. So I switched back to my own jQuery hosting and formed my previously expressed opinion. I used only the first (Google official) method.
Still I do no think that adding one more critical dependency on a third party service to you website is a good idea. "
london builder 13 years ago
Great tips. Many thanks;)