{"id":565,"date":"2014-02-19T18:08:50","date_gmt":"2014-02-19T18:08:50","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=565"},"modified":"2014-02-19T18:08:50","modified_gmt":"2014-02-19T18:08:50","slug":"how-jquery-works","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/how-jquery-works\/","title":{"rendered":"How jQuery Works"},"content":{"rendered":"<h3>jQuery: The Basics<\/h3>\n<p>This is a basic tutorial, designed to help you get started using jQuery. If you don&#8217;t have a test page setup yet, start by creating the following HTML page:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<\/td>\n<td>\n<div>\n<div><code>&lt;!doctype html&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;html&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;head&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;meta charset=\"utf-8\" \/&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;title&gt;Demo&lt;\/title&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/head&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;body&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;a href=\"http:\/\/jquery.com\/\"&gt;jQuery&lt;\/a&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;script src=\"jquery.js\"&gt;&lt;\/script&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;script&gt;<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> \/\/ Your code goes here.<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> &lt;\/script&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/body&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/html&gt;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>The\u00a0<code>src<\/code>\u00a0attribute in the\u00a0<code>&lt;script&gt;<\/code>\u00a0element must point to a copy of jQuery. Download a copy of jQuery from the\u00a0<a href=\"http:\/\/jquery.com\/download\/\">Downloading jQuery<\/a>\u00a0page and store the\u00a0<code>jquery.js<\/code>\u00a0file in the same directory as your HTML file.<\/p>\n<h3><a id=\"launching-code-on-document-ready\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#launching-code-on-document-ready\">link<\/a>Launching Code on Document Ready<\/h3>\n<p>To ensure that their code runs after the browser finishes loading the document, many JavaScript programmers wrap their code in an\u00a0<code>onload<\/code>\u00a0function:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<\/td>\n<td>\n<div>\n<div><code>window.onload = function() {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> alert( \"welcome\" );<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Unfortunately, the code doesn&#8217;t run until all images are finished downloading, including banner ads. To run code as soon as the document is ready to be manipulated, jQuery has a statement known as the\u00a0<a href=\"http:\/\/api.jquery.com\/ready\/\">ready event<\/a>:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( document ).ready(function() {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> \/\/ Your code here.<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>For example, inside the\u00a0<code>ready<\/code>\u00a0event, you can add a click handler to the link:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( document ).ready(function() {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> $( \"a\" ).click(function( event ) {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> alert( \"Thanks for visiting!\" );<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> });<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Save your HTML file and reload the test page in your browser. Clicking the link should now first display an alert pop-up, then continue with the default behavior of navigating to\u00a0<a href=\"http:\/\/jquery.com\/\">http:\/\/jquery.com<\/a>.<\/p>\n<p>For\u00a0<code>click<\/code>\u00a0and most other\u00a0<a href=\"http:\/\/api.jquery.com\/category\/events\/\">events<\/a>, you can prevent the default behavior by calling\u00a0<code>event.preventDefault()<\/code>\u00a0in the event handler:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( document ).ready(function() {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> $( \"a\" ).click(function( event ) {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> alert( \"As you can see, the link no longer took you to jquery.com\" );<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> event.preventDefault();<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> });<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3><a id=\"complete-example\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#complete-example\">link<\/a>Complete Example<\/h3>\n<p>The following example illustrates the click handling code discussed above, embedded directly in the HTML\u00a0<code>&lt;body&gt;<\/code>. Note that in practice, it is usually better to place your code in a separate JS file and load it on the page with a\u00a0<code>&lt;script&gt;<\/code>\u00a0element&#8217;s\u00a0<code>src<\/code>attribute.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<div>8<\/div>\n<div>9<\/div>\n<div>10<\/div>\n<div>11<\/div>\n<div>12<\/div>\n<div>13<\/div>\n<div>14<\/div>\n<div>15<\/div>\n<div>16<\/div>\n<div>17<\/div>\n<div>18<\/div>\n<div>19<\/div>\n<div>20<\/div>\n<div>21<\/div>\n<\/td>\n<td>\n<div>\n<div><code>&lt;!doctype html&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;html&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;head&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;meta charset=\"utf-8\" \/&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;title&gt;Demo&lt;\/title&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/head&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;body&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;a href=\"http:\/\/jquery.com\/\"&gt;jQuery&lt;\/a&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;script src=\"jquery.js\"&gt;&lt;\/script&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code> &lt;script&gt;<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> $( document ).ready(function() {<\/code><\/div>\n<\/div>\n<div>\n<div><code> $( \"a\" ).click(function( event ) {<\/code><\/div>\n<\/div>\n<div>\n<div><code> alert( \"The link will no longer take you to jquery.com\" );<\/code><\/div>\n<\/div>\n<div>\n<div><code> event.preventDefault();<\/code><\/div>\n<\/div>\n<div>\n<div><code> });<\/code><\/div>\n<\/div>\n<div>\n<div><code> });<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> &lt;\/script&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/body&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/html&gt;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3><a id=\"adding-and-removing-an-html-class\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#adding-and-removing-an-html-class\">link<\/a>Adding and Removing an HTML Class<\/h3>\n<p><strong>Important:<\/strong>\u00a0<em>You must place the remaining jQuery examples inside the\u00a0<code>ready<\/code>\u00a0event so that your code executes when the document is ready to be worked on.<\/em><\/p>\n<p>Another common task is adding or removing a class.<\/p>\n<p>First, add some style information into the\u00a0<code>&lt;head&gt;<\/code>\u00a0of the document, like this:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<\/td>\n<td>\n<div>\n<div><code>&lt;style&gt;<\/code><\/div>\n<\/div>\n<div>\n<div><code>a.test {<\/code><\/div>\n<\/div>\n<div>\n<div><code> font-weight: bold;<\/code><\/div>\n<\/div>\n<div>\n<div><code>}<\/code><\/div>\n<\/div>\n<div>\n<div><code>&lt;\/style&gt;<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Next, add the\u00a0<a href=\"http:\/\/api.jquery.com\/addClass\/\">.addClass()<\/a>\u00a0call to the script:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( \"a\" ).addClass( \"test\" );<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>All\u00a0<code>&lt;a&gt;<\/code>\u00a0elements are now bold.<\/p>\n<p>To remove an existing class, use\u00a0<a href=\"http:\/\/api.jquery.com\/removeClass\/\">.removeClass()<\/a>:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( \"a\" ).removeClass( \"test\" );<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3><a id=\"special-effects\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#special-effects\">link<\/a>Special Effects<\/h3>\n<p>jQuery also provides some handy\u00a0<a href=\"http:\/\/api.jquery.com\/category\/effects\/\">effects<\/a>\u00a0to help you make your web sites stand out. For example, if you create a click handler of:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<div>6<\/div>\n<div>7<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$( \"a\" ).click(function( event ) {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> event.preventDefault();<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> $( this ).hide( \"slow\" );<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Then the link slowly disappears when clicked.<\/p>\n<h2><a id=\"callbacks-and-functions\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#callbacks-and-functions\">link<\/a>Callbacks and Functions<\/h2>\n<p>Unlike many other programming languages, JavaScript enables you to freely pass functions around to be executed at a later time. A\u00a0<em>callback<\/em>\u00a0is a function that is passed as an argument to another function and is executed after its parent function has completed. Callbacks are special because they patiently wait to execute until their parent finishes. Meanwhile, the browser can be executing other functions or doing all sorts of other work.<\/p>\n<p>To use callbacks, it is important to know how to pass them into their parent function.<\/p>\n<h3><a id=\"callback-without-arguments\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#callback-without-arguments\">link<\/a>Callback\u00a0<em>without<\/em>\u00a0Arguments<\/h3>\n<p>If a callback has no arguments, you can pass it in like this:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$.get( \"myhtmlpage.html\", myCallBack );<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>When\u00a0<a href=\"http:\/\/api.jquery.com\/jQuery.get\/\">$.get()<\/a>\u00a0finishes getting the page\u00a0<code>myhtmlpage.html<\/code>, it executes the\u00a0<code>myCallBack()<\/code>\u00a0function.<\/p>\n<ul>\n<li><strong>Note:<\/strong>\u00a0The second parameter here is simply the function name (but\u00a0<em>not<\/em>\u00a0as a string, and without parentheses).<\/li>\n<\/ul>\n<h3><a id=\"callback-with-arguments\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#callback-with-arguments\">link<\/a>Callback\u00a0<em>with<\/em>\u00a0Arguments<\/h3>\n<p>Executing callbacks with arguments can be tricky.<\/p>\n<h4><a id=\"wrong\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#wrong\">link<\/a>Wrong<\/h4>\n<p>This code example will\u00a0<strong><em>not<\/em><\/strong>\u00a0work:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$.get( \"myhtmlpage.html\", myCallBack( param1, param2 ) );<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>The reason this fails is that the code executes\u00a0<code>myCallBack( param1, param2 )<\/code>\u00a0immediately and then passes\u00a0<code>myCallBack()<\/code>&#8216;s<em>return value<\/em>\u00a0as the second parameter to\u00a0<code>$.get()<\/code>. We actually want to pass the function\u00a0<code>myCallBack()<\/code>, not\u00a0<code>myCallBack( param1, param2 )<\/code>&#8216;s return value (which might or might not be a function). So, how to pass in\u00a0<code>myCallBack()<\/code>\u00a0<em>and<\/em>\u00a0include its arguments?<\/p>\n<h4><a id=\"right\" href=\"http:\/\/learn.jquery.com\/about-jquery\/how-jquery-works\/#right\">link<\/a>Right<\/h4>\n<p>To defer executing\u00a0<code>myCallBack()<\/code>\u00a0with its parameters, you can use an anonymous function as a wrapper. Note the use of\u00a0<code>function() {<\/code>. The anonymous function does exactly one thing: calls\u00a0<code>myCallBack()<\/code>, with the values of\u00a0<code>param1<\/code>\u00a0and\u00a0<code>param2<\/code>.<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<td>\n<div>1<\/div>\n<div>2<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>5<\/div>\n<\/td>\n<td>\n<div>\n<div><code>$.get( \"myhtmlpage.html\", function() {<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code> myCallBack( param1, param2 );<\/code><\/div>\n<\/div>\n<div><\/div>\n<div>\n<div><code>});<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>When\u00a0<code>$.get()<\/code>\u00a0finishes getting the page\u00a0<code>myhtmlpage.html<\/code>, it executes the anonymous function, which executes\u00a0<code>myCallBack( param1, param2 )<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery: The Basics This is a basic tutorial, designed to help you get started using jQuery. If you don&#8217;t have a test page setup yet, start by creating the following HTML page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 &lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset=&#8221;utf-8&#8243; \/&gt; [&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":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/565"}],"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=565"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/565\/revisions"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}