X

LATEST #JQUERY NEWS

5 Cool and Useful jQuery Tricks


1. Disable Right Click

The code snippet below disables right click on an entire page, but you can make it work on only particular page elements by changing the selector from document to something more specific.

   $(document).ready(function(){
		$(document).bind("contextmenu", function(e){
			return false;
		})
	})

2. Set a Timer
The following code snippet sets off an alert exactly 3 seconds after the page has loaded.

$(document).ready(function(){
		setTimeout(function(){
			alert("Surprise!");
		}, 3000);
	})

3. Browser Detection
jQuery can easily detect which browser a page is being viewed on, and can even be used to apply style or make changes to HTML elements depending on which browser is being used. The code below changes the padding on all p elements when the browser is Safari.

if($.browser.safari) $("p").css("padding", "1em");

4. Dynamically Alter Links
This one is particularly useful if you want to add target=”_blank” to all the links on a particular page without having to go through and do it manually.

$('a[@rel$='external']').click(function(){
  this.target = "_blank";
});

5. Check if Elements Exist
Avoid errors by writing code like this one below that checks if an element exists before it executes the action.

if ($("#someElement").length)
{ 
	//The DOM element exists
}

Recently Published

article image
»

How to Disable Right-Click Menu Using jQuery

There can be dozens of reasons why you may want to disable right ...

article image
»

How to Optimize jQuery Code for Better Performance

jQuery is the most popular client side library, in fact almost every ...

article image
»

Using Head.js to Load All Your JavaScript

In the past few years, client side frameworks/libraries have become ...

article image
»

How to Detect Browser Using jQuery

One of the more frustrating parts of creating a new web project is ...

article image
»

Convert Images to Black and White using jQuery

Converting an image to black and white is no longer a manual event. ...

article image
»

How to Use jQuery to Set Textarea Max Length

If you have a textarea field on any of your sites, it might be a good ...

getTable
»

15 Awesome jQuery Table Plugins

Tables are the most preferred way to display data because rows and ...

article image
»

How to Dynamically Open External Links in New Window

Any web developer will probably know that using the ...

article image
»

How to Use jQuery’s .not() Method

jQuery’s .not() method is a method that allows you to return ...