Who doesn’t like JQuery? This fast and easy to use Javascript framework became very popular in 2008. In the following article, I have compiled a list of 8 absolutely useful JQuery hacks, tips and tricks.
Target blank links
Do you use the target=blank attribute on links? If yes, you might know that XHTML 1.0 Strict don’t allow it. A good solution to this problem should be using JQuery to make links opening in new windows:
$('a[@rel$='external']').click(function(){ this.target = "_blank"; }); /* Usage: <a href="http://www.lepinskidesign.com.br/" rel="external">lepinskidesign.com.br</a> */
Get the total number of matched elements
That what I call a very simple, but very useful tip: This will return the number of matched elements:
$('element').size();
Preloading images
When you’re using images in Javascript, a good thing is to preload it before you have to use it. This code will do the job:
jQuery.preloadImages = function() { for(var i = 0; i").attr("src", arguments[i]); } }; // Usage $.preloadImages("image1.gif", "/path/to/image2.png", "some/image3.jpg"); </ARGUMENTS.LENGTH;>
Detect browser
Although it is better to use CSS conditionnal comments to detect a specific browser and apply some css style, it is a very easy thing to do with JQuery, which can be useful at times.
//A. Target Safari if( $.browser.safari ) $("#menu li a").css("padding", "1em 1.2em" ); //B. Target anything above IE6 if ($.browser.msie && $.browser.version > 6 ) $("#menu li a").css("padding", "1em 1.8em" ); //C. Target IE6 and below if ($.browser.msie && $.browser.version <= 6 ) $("#menu li a").css("padding", "1em 1.8em" ); //D. Target Firefox 2 and above if ($.browser.mozilla && $.browser.version >= "1.8" ) $("#menu li a").css("padding", "1em 1.8em" );
Remove a word in a text
Do you ever wanted to be able to remove words in a text? Note that the following code can be easily modified to replace a word by another.
var el = $('#id'); el.html(el.html().replace(/word/ig, ""));
Columns of equal height
This seems to be a highly-requested hack: How to use two CSS columns, and make them having exactly the same height? Happilly Rob from cssnewbie have the solution.
function equalHeight(group) { tallest = 0; group.each(function() { thisHeight = $(this).height(); if(thisHeight > tallest) { tallest = thisHeight; } }); group.height(tallest); } /* Usage: $(document).ready(function() { equalHeight($(".recent-article")); equalHeight($(".footer-col")); }); */
Source: Equal Height Columns with jQuery
Font resizing
Font Resizing is a very common feature in many modern websites. Here’s how to do it with JQuery.
$(document).ready(function(){ // Reset Font Size var originalFontSize = $('html').css('font-size'); $(".resetFont").click(function(){ $('html').css('font-size', originalFontSize); }); // Increase Font Size $(".increaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*1.2; $('html').css('font-size', newFontSize); return false; }); // Decrease Font Size $(".decreaseFont").click(function(){ var currentFontSize = $('html').css('font-size'); var currentFontSizeNum = parseFloat(currentFontSize, 10); var newFontSize = currentFontSizeNum*0.8; $('html').css('font-size', newFontSize); return false; }); });
Source: Text Resizing With jQuery
Disable right-click contextual menu
There’s many Javascript snippets available to disable right-click contextual menu, but JQuery makes things a lot easier:
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }); });
Source: Fast Tip: How to cancel right click context menu in jQuery