Following are few very useful jQuery Tips and Tricks for all jQuery developers. I am sharing these as I think they will be very useful to you. Disclaimer: I have not written all of the below code but have collected from various sources from Internet.
1. Optimize performance of complex selectors
Query a subset of the DOM when using complex selectors drastically improves performance:
var subset = $("");
$("input[value^='']", subset);
Code language:JavaScript(javascript)
2. Set Context and improve the performance
On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, rather than from the DOM root. Given a large enough DOM, specifying the context parameter should translate to performance gains.
$("input:radio", document.forms[0]);
Code language:JavaScript(javascript)
3. Live Event Handlers
Set an event handler for any element that matches a selector, even if it gets added to the DOM after the initial page load:
This allows you to load content via ajax, or add them via javascript and have the event handlers get set up properly for those elements automatically. Likewise, to stop the live event handling:
$('button.someClass').die('click', someFunction);
Code language:JavaScript(javascript)
These live event handlers have a few limitations compared to regular events, but they work great for the majority of cases. Live event will work starting from jQuery 1.3
4. Checking the Index
jQuery has .index but it is a pain to use as you need the list of elements and pass in the element you want the index of
var index = e.g $('#ul>li').index( liDomObject );
Code language:JavaScript(javascript)
The following is easier: if you want to know the index of an element within a set, e.g. list items within a unordered list:
$("ul > li").click(function ()
{
var index = $(this).prevAll().length;
});
Code language:JavaScript(javascript)
5. Use jQuery data method
jQuery’s data() method is useful and not well known. It allows you to bind data to DOM elements without modifying the DOM.
6. Fadeout Slideup effect to remove an element
Combine more than one effects in jQuery to animate and remove an element from DOM.
$("#myButton").click(function() {
$("#myDiv").fadeTo("slow", 0.01, function(){ //fade
$(this).slideUp("slow", function() { //slide up
$(this).remove(); //then remove from the DOM
});
});
});
Code language:JavaScript(javascript)
7. Checking if an element exists
Use following snippet to check whether an element exists or not.
if ($("#someDiv").length) {
//hooray!!! it exists...
}
We use .hide(), .show() methods in jquery to change the visibility of an element. Use following code to check the whether an element is visible or not.
if($(element).is(":visible") == "true") {
//The element is Visible
}
This script will display the x and y value – the coordinate of the mouse pointer.
$().mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
<p></p>
number 7 – while that does work, you don’t need to check whether elements exist before selecting them. jQuery will not throw errors if you try to select elements not on the page (unlike most other libraries). Unnecessary checking of whether elements exist will slow things down a little.
number 9 – this is in no way related to performance, in fact, it may even make scripts slower! Compression applications, such as YUI compressor, remove line-breaks from JS documents when you compress them. Line breaks do need to be processed and exist as characters, so unecessary line breaks are going to slow things down (even only by a few milliseconds)
I’ve been using jQuery for about as long as it has existed (about 3-4 years) and I never knew there was “contextmenu” event. Never had to use it… but, it’s nice to know it exists.
Nice 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
Thank you a lot for giving everyone an exceptionally nice possiblity to read articles and blog posts from here. It can be so nice and as well , full of a great time for me personally and my office co-workers to visit your website at minimum 3 times a week to find out the newest guides you have. And indeed, I’m also always impressed for the unbelievable techniques served by you. Some 3 ideas in this article are undeniably the simplest I have had.
I must admit it is a great list indeed. I am collecting jquery tips and tricks. I follow a jquery blog called jquerybyexample.blogspot.com. Recently this guy has also posted about top jquery tips and tricks. few are similar to yours but rest of them are really worth.
Its great that you collected most commnly used tricks of JQuery. I have been using few of them and this list really helped me. One of the common use is to make select the first drop down.
$(“target option:first”).attr(‘selected’,’selected’); or $(“#target”).val($(“#target option:first”).val());
Thanks for sharing these tips, very useful. I think readers would find this 5 jquery performance tips useful:
http://jquery-howto.blogspot.com/2009/02/5-easy-tips-on-how-to-improve-code.html
Great list :D
Couple of things though:
number 7 – while that does work, you don’t need to check whether elements exist before selecting them. jQuery will not throw errors if you try to select elements not on the page (unlike most other libraries). Unnecessary checking of whether elements exist will slow things down a little.
number 9 – this is in no way related to performance, in fact, it may even make scripts slower! Compression applications, such as YUI compressor, remove line-breaks from JS documents when you compress them. Line breaks do need to be processed and exist as characters, so unecessary line breaks are going to slow things down (even only by a few milliseconds)
But some great tips apart from those :)
@Andrew, Thanks for the comment and thanks for sharing the useful link.
@Dan, Great insight and thanks for the comment :) Glad you liked the snippets. Feel free to go through rest of the articles of this site.
Great list, thanks. I’m may quesion #17, how to replicate being in top 20 but I’d just be quibbling :)
@Dan: Regarding #9, you must admit that the virtually unnoticeable drop in speed is well worth the upgrade to readability
Good list! thanks for sharing
@Vipul, Thanks for the comment. Happy Reading :)
I’ve been using jQuery for about as long as it has existed (about 3-4 years) and I never knew there was “contextmenu” event. Never had to use it… but, it’s nice to know it exists.
The item 13 is not good in performance.
Nice 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
http://technosiastic.wordpress.com/2009/09/24/collection-of-top-jquery-tutorials-tips-tricks-techniques-to-improve-performance/
The tutor is very good …….
very powerful
Thank you!happy reading…
@willy: you welcome
Nice tutorial.
For more Jquery tutorials visit webspeaks.in
I tried #16 and it is not working. I found out that jQuery does not allow color property to be changed in .animate(). Any idea?
You’ll need the jQuery UI code to animate non-numeric values now. You don’t need all the jQUI theme stuff, just the JS file.
Its very useful information..good one.
Thanks for this list. especially for #17. Great tips!
Nice tips and guides. Very useful.
Thanks for the list.
Excellent art of knowledge. :-)
yaa reaaaly nice
Interesting alternative for the page loaded function, nice work!
Thank you a lot for giving everyone an exceptionally nice possiblity to read articles and blog posts from here. It can be so nice and as well , full of a great time for me personally and my office co-workers to visit your website at minimum 3 times a week to find out the newest guides you have. And indeed, I’m also always impressed for the unbelievable techniques served by you. Some 3 ideas in this article are undeniably the simplest I have had.
Keep working ,great job!
Nice article. I also wrote a small article about this subject http://www.prideparrot.com/blog/archive/2011/9/interesting_things_from_jquery
I like #19 will defenatly use it
Instead of using return false use e.preventDefault();
Returning false is a very bad practice.
great tips really helpful
Great post.. But today i found something new about how to use selectors efficiently, which is really a great post indeed..
Thought of sharing with your reader.
http://jquerybyexample.blogspot.com/2011/11/tips-to-use-jquery-selectors.html
Thanks,
Virendra
I must admit it is a great list indeed. I am collecting jquery tips and tricks. I follow a jquery blog called jquerybyexample.blogspot.com. Recently this guy has also posted about top jquery tips and tricks. few are similar to yours but rest of them are really worth.
http://jquerybyexample.blogspot.com/2011/12/best-jquery-tips-and-tricks.html
And yes forgot to mention that I have started following your blog as well…
Its great that you collected most commnly used tricks of JQuery. I have been using few of them and this list really helped me. One of the common use is to make select the first drop down.
$(“target option:first”).attr(‘selected’,’selected’);
or
$(“#target”).val($(“#target option:first”).val());
Awesome tips specially 16 and17
Really good and usefull collection. Thank you.
Great tips! Trying to get a grasp on jQuery and found that very useful.
This is awesome. It will be very helpful for the beginners like me.
Thank you very much for giving such as a wonderful article/
#6 can be done like this – using delay().
$(“#myButton”).click(function() {
$(“#myDiv”).fadeTo(500, 0.01).delay(500).slideUp(500).delay(500).remove();
});
Regarding #19, don’t ever do that. That’s actively user hostile. No no no.
Man you’re awesome! did you know about it? ;)
very well-organized collection, thanks dude.
in 15. with this method :
you have $(“#foo > div”).size() = 1
$.fn.live() is dperecated. Please don’t use it.
This is a great jquery tutorials, thanks for sharing!
Very helpful! I found 1 mistake though.
Instead of:
you should use:
Especially if you’re working with json data (I had a json list), it messes it up:
This:
becomes this:
Thanks you, it is a good tips,
nice work
Thanks for your valuable information. This clears the base of every developer with ease practises.
Hope this link makes a quite effect on you:
http://www.namasteui.com/tips-and-tricks-for-powerful-developers/
—
Regards,
Sourav Basak [Blogger, Entrepreneur, Thinker]
http://www.namasteui.com