var subset = $("");
$("input[value^='']", subset);
Code language: JavaScript (javascript)
$("input:radio", document.forms[0]);
Code language: JavaScript (javascript)
$('button.someClass').live('click', someFunction);
Code language: JavaScript (javascript)
This allows you to load content via ajax, or add them via javascript and have the event handlers get set up properly for those elements automatically. Likewise, to stop the live event handling: $('button.someClass').die('click', someFunction);
Code language: JavaScript (javascript)
These live event handlers have a few limitations compared to regular events, but they work great for the majority of cases. Live event will work starting from jQuery 1.3 var index = e.g $('#ul>li').index( liDomObject );
Code language: JavaScript (javascript)
The following is easier: if you want to know the index of an element within a set, e.g. list items within a unordered list: $("ul > li").click(function ()
{
var index = $(this).prevAll().length;
});
Code language: JavaScript (javascript)
$("#myButton").click(function() {
$("#myDiv").fadeTo("slow", 0.01, function(){ //fade
$(this).slideUp("slow", function() { //slide up
$(this).remove(); //then remove from the DOM
});
});
});
Code language: JavaScript (javascript)
if ($("#someDiv").length) {
//hooray!!! it exists...
}
Code language: JavaScript (javascript)
var newDiv = $('<div></div>');
newDiv.attr("id","myNewDiv").appendTo("body");
Code language: JavaScript (javascript)
$("a").hide().addClass().fadeIn().hide();
Code language: JavaScript (javascript)
You can increase readability like so: $("a")
.hide()
.addClass()
.fadeIn()
.hide();
Code language: JavaScript (javascript)
$.extend($.expr[':'], {
over100pixels: function(a) {
return $(a).height() > 100;
}
});
$('.box:over100pixels').click(function() {
alert('The element you clicked is over 100 pixels high');
});
Code language: JavaScript (javascript)
// Clone the DIV
var cloned = $('#somediv').clone();
Code language: JavaScript (javascript)
jQuery’s clone() method does not clone a JavaScript object. To clone JavaScript object, use following code. // Shallow copy
var newObject = jQuery.extend({}, oldObject);
// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);
Code language: JavaScript (javascript)
if($(element).is(":visible") == "true") {
//The element is Visible
}
Code language: JavaScript (javascript)
//Instead of
$(document).ready(function() {
//document ready
});
//Use
$(function(){
//document ready
});
Code language: JavaScript (javascript)
$("#Address\\.Street").text("Enter this field");
Code language: JavaScript (javascript)
<div id="foo">
<div id="bar"></div>
<div id="baz">
<div id="biz">
</div>
<span><span>
</div>
//jQuery code to count child elements
$("#foo > div").size()
Code language: JavaScript (javascript)
jQuery.fn.flash = function( color, duration )
{
var current = this.css( 'color' );
this.animate( { color: 'rgb(' + color + ')' }, duration / 2 );
this.animate( { color: current }, duration / 2 );
}
//Then use the above function as:
$( '#importantElement' ).flash( '255,0,0', 1000 );
Code language: JavaScript (javascript)
jQuery.fn.center = function () {
this.css("position","absolute");
this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
return this;
}
//Use the above function as:
$(element).center();
Code language: JavaScript (javascript)
$("#searchBox").closest("div");
Code language: JavaScript (javascript)
$(document).ready(function(){
$(document).bind("contextmenu",function(e){
return false;
});
});
Code language: JavaScript (javascript)
$().mousemove(function(e){
//display the x and y axis values inside the P element
$('p').html("X Axis : " + e.pageX + " | Y Axis " + e.pageY);
});
<p></p>
Code language: JavaScript (javascript)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks for sharing these tips, very useful. I think readers would find this 5 jquery performance tips useful:
http://jquery-howto.blogspot.com/2009/02/5-easy-tips-on-how-to-improve-code.html
Great list :D
Couple of things though:
number 7 - while that does work, you don't need to check whether elements exist before selecting them. jQuery will not throw errors if you try to select elements not on the page (unlike most other libraries). Unnecessary checking of whether elements exist will slow things down a little.
number 9 - this is in no way related to performance, in fact, it may even make scripts slower! Compression applications, such as YUI compressor, remove line-breaks from JS documents when you compress them. Line breaks do need to be processed and exist as characters, so unecessary line breaks are going to slow things down (even only by a few milliseconds)
But some great tips apart from those :)
@Andrew, Thanks for the comment and thanks for sharing the useful link.
@Dan, Great insight and thanks for the comment :) Glad you liked the snippets. Feel free to go through rest of the articles of this site.
Great list, thanks. I'm may quesion #17, how to replicate being in top 20 but I'd just be quibbling :)
@Dan: Regarding #9, you must admit that the virtually unnoticeable drop in speed is well worth the upgrade to readability
Good list! thanks for sharing
@Vipul, Thanks for the comment. Happy Reading :)
I've been using jQuery for about as long as it has existed (about 3-4 years) and I never knew there was "contextmenu" event. Never had to use it... but, it's nice to know it exists.
The item 13 is not good in performance.
Nice collection of jQuery Tips and Tricks mate. I have also linked to yours from my blog post below where I am trying to collect the most useful and common jQuery code snippets for JavaScript over the web. Here is the title and the link to the jQuery link compilation endeavor:
Ultimate collection of top jQuery tutorials, tips-tricks and techniques to improve performance
http://technosiastic.wordpress.com/2009/09/24/collection-of-top-jquery-tutorials-tips-tricks-techniques-to-improve-performance/