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
»

jQuery Snippets: How to Add/Remove Classes After Delay

In your projects, there can often be instances of wanting to change ...

article image
»

jQuery Snippets: Back to Top Button

One common way to use jQuery is to add a useful and functional ...

article image
»

jQuery .on() Method: the Issue of Dynamically Added Elements

The jQuery .on() method is the recommended way for attaching events ...

article image
»

How to Use jQuery to Test CSS3 Browser Support

The tricky thing about using CSS properties (especially newer ones ...

article image
»

jQuery Snippets: Check/Un-Check All Function

This easy-to-implement jQuery code snippet is super useful, and can ...

article image
»

Useful jQuery Code Snippets for Handling CSS

If you’re not already familiar with code snippets, they are ...

article image
»

Using Method Chaining in jQuery

Using method chaining in jQuery helps to ensure that your code stays ...

article image
»

jQuery’s .children() Method

Just like in CSS, it’s possible to use jQuery to select the ...

article image
»

jQuery’s .click() Method

With jQuery, it’s super easy to make something happen as a result ...