10 Jquery Snippets
10 Jquery Snippets
htm
I have found that people are going bananas over jQuery, so i’ve decided to share my top 10 jQuery
snippets that i KNOW you will need to use at one point or another. [...]
Amazing collection of the top 10 most used jQuery snippets that you need to know about! Includes new
amazing 1.4 jQuery framework methods.
I have found that people are going bananas over jQuery, so i’ve decided to share my top 10 jQuery
snippets that i KNOW you will need to use at one point or another. So here they are, enjoy! I have also
added jquery 1.4 features never used before.
Almost every time I start using jQuery I need to paste this in to start writing my beautiful code.
You can either paste this in your header (if you are using jquery for any appearance changes its suggested
to do so in the header) or you can add it at the end of your body, that way your styles will load first (hence
making a faster load) and then the jquery will be loaded last.
I find myself using this rather often, whenever you have a search field and you want it to defaultly display
a value “search…” and have that value empty out on focus, well this is how to do this with jQuery 1.4.
This jquery will create an array with every input field that has the class swap. For example:
The value Swap Me! will be erased when you click in the input field, and it will come back in if you don’t
enter anything.
Unfortunately css does not support this natively, sometimes when creating column based layouts it looks
so much better if the columns aligned by height, so here is the easy way of doing this.
This will go through every div with class col and check for which one contains the highest size, once done
it will apply that height to all divs with class col. For example:
If you read the comments above you will see that each div has the class col, and that the second div will
be set at height 200px.
Let’s say you want to have a suckerfish dropdown, or maybe you just want to add a cool hover on a div,
well in some browsers thats not allowed to be done with pure css, hence use this simple technique and you
will have cross browser hover with any two class you want.
01. $('.onhover').hover(
02. function(){ $(this).addClass('hover_style_class') },
03. function(){ $(this).removeClass('hover_style_class') }
04. )
The code above will apply a class home_style_class on hover of an element with the class onhover.
With jquery you can capture an onclick event with the .click() method, but this is the slower less efficient
way. Also if you created new elements with ajax or with jquery those new elements will not be bound by
the regular click() method. So using live click/submit and so on will apply itself to all new elements, and
will only bind once to all.
The code above will apply any code you want on click event on an element with class element.
Okay personally when the term ajax comes to mind i get a little worried… but I found this amazing
technique to refresh page content without any real ajax or special tricks. All you need is to apply an id to a
certain element, and run this little script, choose how many seconds to wait until refresh and VUALA!
01. setInterval(function() {
02. $("#refresh").load(location.href+" #refresh>*","");
03. }, 10000); // seconds to wait, miliseconds
Okay so the script above will refresh the div with id refresh every 10 seconds… this can be so awesome in
so many cases! Btw make sure you have tags inside the refresh div, otherwise it doesn’t seem to work
with pure text.
7. Each Element
I seem to use this pretty often when i want to do something with a set of elements, for example if you do
your own form validation and you want to check each input field before submitting.
So choose an element to cycle through, it can be a set of li’s in a chosen unordered list, or all the input
fields as above. Use the i to get the number of the current element, and of course the $(this) to do
something to the element it’s going through.
With the new jQuery 1.4 we have the new awesome feature of the closest() method which will find the
element closes to the current one. So no more searching through the parents and children to get to the
close element we need.
01. $("li").click(function(){
02. $(this).closest(".me").addClass("smile");
03. });
The code above will find the closes class me to the li that was clicked on and add class smile to it. Hence
you can see how beneficial this can be!
9. Delay animation/effect
Another great new feature of the 1.4 jquery framework is the Delay method which allows you to delay an
animation. Instead of using the setTimeout method, you can now simply add the .delay() method and pass
in how long to delay, like this:
01. $(".alert").delay(2000).fadeOut();
The above will fade out an element with class alert after 2 seconds. This can be used for growl like
notifications.
Another awesome feature of jQuery 1.4 that will be quite handy is the has method. This method will find
if a an element contains a certain other element class or whatever it is you are looking for and do anything
you want to them.
01. $("input").has(".email").addClass("email_icon");
In the above code we will look through all inputs that have the class email and add the class email_icon.
Sometimes we have tables or ordered/unordered lists that we want to be easily read. Here is a quick jq
trick:
01. $("tr:odd").addClass("odd");
02. $("li:odd").addClass("odd");
The code will add the class odd to every other element!
Tired of having to add target=”_blank” every time you don’t want your visitors to navigate away from
your page? Let jquery do the work!
01. $('a').each(function() {
02. var a = new RegExp('/' + [removed].host + '/');
03. if(!a.test(this.href)) {
04. $(this).click(function(event) {
05. event.preventDefault();
06. event.stopPropagation();
07. window.open(this.href, '_blank');
08. });
09. }
10. });
Now every a href in your page will open in a new window if it doesnt go somewhere in your own website!