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’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 ...

article image
»

Using jQuery’s .resize() Method

jQuery’s .resize() method is a cool way to have an event ...

article image
»

How to Use jQuery to Search and Replace HTML

Have you ever made a mistake in your code or in your text and dreaded ...

article image
»

How to Use jQuery’s .mouseover() Method

jQuery’s .mouseover() method is used to execute some jQuery ...

article image
»

How to Use jQuery’s .blur() Method

jQuery’s .blur() method isn’t exactly what it sounds like ...

article image
»

jQuery 3.0: Fix url.indexOf is Not a Function Error

jQuery 3.0 is out, and if you started working with it and plan to ...

article image
»

Set/Get/Delete Cookies with jQuery

There are a lot of plugins you can use to set, get, and delete ...

Screen Shot 2016-11-26 at 12.05.09 PM
»

3 Best jQuery Video Gallery Plugins

Do you have a lot of videos that you’d like to embed or neatly ...