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
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...

article image
»

Check all checkboxes in HTML table using jQuery

HTML tables are a very handy and useful element. They can adapt to ...

article image
»

Changing images in a loop on hover using jQuery

Why use this Function? Changing images in a loop on mouse over is a ...

article image
»

How to Dynamically Trim Text Using jQuery

This easy beginners jQuery tutorial demonstrates how you can use ...

article image
»

jQuery Snippets: Getting the Current Year

Here’s a fun, simple, and incredibly useful jQuery that you can ...

article image
»

jQuery Snippets: How to Conditionally Enable Submit

One easy way to prevent users from submitting an empty form in a ...

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...