{"id":207,"date":"2013-01-18T15:27:26","date_gmt":"2013-01-18T15:27:26","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=207"},"modified":"2013-01-18T15:27:26","modified_gmt":"2013-01-18T15:27:26","slug":"top-jquery-tips-tricks-jquery-programmers","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/top-jquery-tips-tricks-jquery-programmers\/","title":{"rendered":"Top jQuery Tips &#038; Tricks for jQuery Programmers"},"content":{"rendered":"<h2>Optimize performance of complex selectors<\/h2>\n<p>Query a subset of the DOM when using complex selectors drastically improves performance:<\/p>\n<div>\n<div id=\"highlighter_571942\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>var<\/code> <code>subset = $(<\/code><code>\"\"<\/code><code>);<\/code><\/div>\n<div><code>$(<\/code><code>\"input[value^='']\"<\/code><code>, subset);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Set Context and improve the performance<\/h2>\n<p>On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, rather than from the DOM root. Given a large enough DOM, specifying the context parameter should translate to performance gains.<\/p>\n<div>\n<div id=\"highlighter_99996\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"input:radio\"<\/code><code>, document.forms[0]);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Live Event Handlers<\/h2>\n<p>Set an event handler for any element that matches a selector, even if it gets added to the DOM after the initial page load:<\/p>\n<div>\n<div id=\"highlighter_651942\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>'button.someClass'<\/code><code>).live(<\/code><code>'click'<\/code><code>, someFunction);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>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.<\/p>\n<p>Likewise, to stop the live event handling:<\/p>\n<div>\n<div id=\"highlighter_26047\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>'button.someClass'<\/code><code>).die(<\/code><code>'click'<\/code><code>, someFunction);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>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<\/p>\n<h2>Checking the Index<\/h2>\n<p>jQuery has .index but it is a pain to use as you need the list of elements and pass in the element you want the index of<\/p>\n<div>\n<div id=\"highlighter_402015\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>var<\/code> <code>index = e.g $(<\/code><code>'#ul&gt;li'<\/code><code>).index( liDomObject );<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The following is easier:<\/p>\n<p>if you want to know the index of an element within a set, e.g. list items within a unordered list:<\/p>\n<div>\n<div id=\"highlighter_230353\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"ul &gt; li\"<\/code><code>).click(<\/code><code>function<\/code> <code>()<\/code><\/div>\n<div><code>{<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>var<\/code> <code>index = $(<\/code><code>this<\/code><code>).prevAll().length;<\/code><\/div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Use jQuery data method<\/h2>\n<p>jQuery\u2019s <a href=\"http:\/\/docs.jquery.com\/Core\/data\" target=\"_new\" rel=\"nofollow\">data() method<\/a> is useful and not well known. It allows you to bind data to DOM elements without modifying the DOM.<\/p>\n<h2>Fadeout Slideup effect to remove an element<\/h2>\n<p>Combine more than one effects in jQuery to animate and remove an element from DOM.<\/p>\n<div>\n<div id=\"highlighter_806437\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"#myButton\"<\/code><code>).click(<\/code><code>function<\/code><code>() {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>$(<\/code><code>\"#myDiv\"<\/code><code>).fadeTo(<\/code><code>\"slow\"<\/code><code>, 0.01, <\/code><code>function<\/code><code>(){ <\/code><code>\/\/fade<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>$(<\/code><code>this<\/code><code>).slideUp(<\/code><code>\"slow\"<\/code><code>, <\/code><code>function<\/code><code>() { <\/code><code>\/\/slide up<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>$(<\/code><code>this<\/code><code>).remove(); <\/code><code>\/\/then remove from the DOM<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>});<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>});<\/code><\/div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Checking if an element exists<\/h2>\n<p>Use following snippet to check whether an element exists or not.<\/p>\n<div>\n<div id=\"highlighter_890090\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>if<\/code> <code>($(<\/code><code>\"#someDiv\"<\/code><code>).length) {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>\/\/hooray!!! it exists...<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Add dynamically created elements into the DOM<\/h2>\n<p>Use following code snippet to create a DIV dynamically and add it into the DOM.<br \/>\n<strong>Further Reading:<\/strong> <a href=\"http:\/\/viralpatel.net\/blogs\/dynamically-add-remove-rows-in-html-table-using-javascript\/\">Dynamically Add\/Remove rows in HTML table using JavaScript<\/a><\/p>\n<div>\n<div id=\"highlighter_165014\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>var<\/code> <code>newDiv = $(<\/code><code>'&lt;div&gt;&lt;\/div&gt;'<\/code><code>);<\/code><\/div>\n<div><code>newDiv.attr(<\/code><code>\"id\"<\/code><code>,<\/code><code>\"myNewDiv\"<\/code><code>).appendTo(<\/code><code>\"body\"<\/code><code>);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Line breaks and chainability<\/h2>\n<p>Instead of doing:<\/p>\n<div>\n<div id=\"highlighter_698490\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"a\"<\/code><code>).hide().addClass().fadeIn().hide();<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>You can increase readability like so:<\/p>\n<div>\n<div id=\"highlighter_772495\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"a\"<\/code><code>)<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>.hide()<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>.addClass()<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>.fadeIn()<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>.hide();<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Creating custom selectors<\/h2>\n<div>\n<div id=\"highlighter_602039\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$.extend($.expr[<\/code><code>':'<\/code><code>], {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>over100pixels: <\/code><code>function<\/code><code>(a) {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>return<\/code> <code>$(a).height() &gt; 100;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>});<\/code><\/div>\n<div><\/div>\n<div><code>$(<\/code><code>'.box:over100pixels'<\/code><code>).click(<\/code><code>function<\/code><code>() {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>alert(<\/code><code>'The element you clicked is over 100 pixels high'<\/code><code>);<\/code><\/div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Cloning an object in jQuery<\/h2>\n<p>Use .clone() method of jQuery to clone any DOM object in JavaScript.<\/p>\n<div>\n<div id=\"highlighter_192166\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>\/\/ Clone the DIV<\/code><\/div>\n<div><code>var<\/code> <code>cloned = $(<\/code><code>'#somediv'<\/code><code>).clone();<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>jQuery\u2019s clone() method does not clone a JavaScript object. To clone JavaScript object, use following code.<\/p>\n<div>\n<div id=\"highlighter_965471\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>\/\/ Shallow copy<\/code><\/div>\n<div><code>var<\/code> <code>newObject = jQuery.extend({}, oldObject);<\/code><\/div>\n<div><\/div>\n<div><code>\/\/ Deep copy<\/code><\/div>\n<div><code>var<\/code> <code>newObject = jQuery.extend(<\/code><code>true<\/code><code>, {}, oldObject);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Test if something is hidden using jQuery<\/h2>\n<p>We use .hide(), .show() methods in jquery to change the visibility of an element. Use following code to check the whether an element is visible or not.<\/p>\n<div>\n<div id=\"highlighter_335533\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>if<\/code><code>($(element).is(<\/code><code>\":visible\"<\/code><code>) == <\/code><code>\"true\"<\/code><code>) {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>\/\/The element is Visible<\/code><\/div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Alternate way of Document Ready<\/h2>\n<div>\n<div id=\"highlighter_479727\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>\/\/Instead of<\/code><\/div>\n<div><code>$(document).ready(<\/code><code>function<\/code><code>() {<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>\/\/document ready<\/code><\/div>\n<div><code>});<\/code><\/div>\n<div><code>\/\/Use<\/code><\/div>\n<div><code>$(<\/code><code>function<\/code><code>(){<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>\/\/document ready<\/code><\/div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Selecting an element with . (period) in its ID<\/h2>\n<p>Use backslash in the selector to select the element having period in its ID.<\/p>\n<div>\n<div id=\"highlighter_724205\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>$(<\/code><code>\"#Address\\\\.Street\"<\/code><code>).text(<\/code><code>\"Enter this field\"<\/code><code>);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Counting immediate child elements<\/h2>\n<p>If you want to count all the DIVs present in the element #foo<\/p>\n<div>\n<div id=\"highlighter_568050\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>&lt;div id=<\/code><code>\"foo\"<\/code><code>&gt;<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>&lt;div id=<\/code><code>\"bar\"<\/code><code>&gt;&lt;\/div&gt;<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>&lt;div id=<\/code><code>\"baz\"<\/code><code>&gt;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>&lt;div id=<\/code><code>\"biz\"<\/code><code>&gt;<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>&lt;\/div&gt;<\/code><\/div>\n<div><code>\u00a0\u00a0<\/code><code>&lt;span&gt;&lt;span&gt;<\/code><\/div>\n<div><code>&lt;\/div&gt;<\/code><\/div>\n<div><\/div>\n<div><code>\/\/jQuery code to count child elements<\/code><\/div>\n<div><code>$(<\/code><code>\"#foo &gt; div\"<\/code><code>).size()<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<h2>Make an element to \u201cFLASH\u201d<\/h2>\n<div>\n<div><code>jQuery.fn.flash = <\/code><code>function<\/code><code>( color, duration )<\/code><\/div>\n<div><code>{<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>var<\/code> <code>current = <\/code><code>this<\/code><code>.css( <\/code><code>'color'<\/code> <code>);<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>this<\/code><code>.animate( { color: <\/code><code>'rgb('<\/code> <code>+ color + <\/code><code>')'<\/code> <code>}, duration \/ 2 );<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>this<\/code><code>.animate( { color: current }, duration \/ 2 );<\/code><\/div>\n<div><code>}<\/code><\/div>\n<div><code>\/\/Then use the above function as:<\/code><\/div>\n<div><code>$( <\/code><code>'#importantElement'<\/code> <code>).flash( <\/code><code>'255,0,0'<\/code><code>, 1000 );<\/code><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Optimize performance of complex selectors Query a subset of the DOM when using complex selectors drastically improves performance: var subset = $(&#8220;&#8221;); $(&#8220;input[value^=&#8221;]&#8221;, subset); Set Context and improve the performance On the core jQuery function, specify the context parameter when. Specifying the context parameter allows jQuery to start from a deeper branch in the DOM, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[9,6,7],"tags":[117],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/207"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=207"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/207\/revisions"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}