{"id":2164,"date":"2023-05-02T18:17:09","date_gmt":"2023-05-02T22:17:09","guid":{"rendered":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-fall-21\/?p=2164"},"modified":"2023-05-02T18:17:14","modified_gmt":"2023-05-02T22:17:14","slug":"jquery-basics","status":"publish","type":"post","link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/topics\/javascript\/jquery-basics\/","title":{"rendered":"jQuery Basics"},"content":{"rendered":"\n<p>jQuery is the most popular JavaScript Library. Using a library allows you to quickly write code that is compatible across a range of browsers. <\/p>\n\n\n\n<p>It should be noted that recently browsers and JavaScript have evolved to make it possible to do a lot of the things jQuery is good at without jQuery. You may see posts and other writing recommending what is referred to as &#8220;vanilla&#8221; JavaScript.  Check out the <a href=\"https:\/\/youmightnotneedjquery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">You Might not Need jQuery<\/a> site to see a comparison. Learning vanilla JS is great, will help you do other things with JS and save your users downloading the jQuery library. <\/p>\n\n\n\n<p>However, it does involve writing more, and more complex code, and is a bit longer to learn at first. So we&#8217;re using jQuery as an entry point in this class. If you want a lighter weight (smaller filesize) version try the <a rel=\"noreferrer noopener\" href=\"https:\/\/zeptojs.com\/\" target=\"_blank\">Zepto<\/a> library.<\/p>\n\n\n\n<p>To learn JavaScript more fully, try a combination of sites like Wes Bos&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/wesbos.com\/javascript\" target=\"_blank\">Beginner JavaScript<\/a>, LinkedIn Learning JavaScript courses, YouTube videos and <a rel=\"noreferrer noopener\" href=\"https:\/\/stackoverflow.com\/\" target=\"_blank\">Stack Overflow<\/a> for questions. Learning to use these types of resources together will help throughout your career.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">About this Post<\/h2>\n\n\n\n<p>This page outlines jQuery code to write in a .js file. For more info on getting the jQuery library set up and ready to go see this <a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/topics\/javascript\/jquery-setup\/\" data-type=\"post\" data-id=\"2105\">jQuery Setup<\/a> post.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Comments<\/h3>\n\n\n\n<p>Typing two slashes \/\/ tells JavaScript to ignore what comes after it on the line. This lets you add comments to your code to help document what it does. You&#8217;ll see those below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ this is a comment and is ignored by JS<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">When Your Code Runs<\/h2>\n\n\n\n<p>There are two basic times when code runs:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Immediately (or at least as soon as the html file loads).<\/li>\n\n\n\n<li>When the user does something.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Immediately<\/h3>\n\n\n\n<p>To run the code as soon as the document is ready, just put it inside the document ready function. Here is an example where all elements with the .accordion-item class are hidden on page load.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">$(document).ready(function() {\n  \/\/ do stuff immediately when DOM is ready\n  $('.accordion-item').hide();\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">When the User Does Something<\/h3>\n\n\n\n<p>When the user does something in the browser it&#8217;s called an <strong>event.<\/strong>To respond to the user doing something, like clicking a button, we use what is called an event handler. An <strong>event handler<\/strong> is a function(). In the example below we&#8217;re responding to the user <strong>clicking<\/strong> an element with the .accordion-header class.<\/p>\n\n\n\n<p>.<a href=\"https:\/\/api.jquery.com\/click\/\" target=\"_blank\" rel=\"noreferrer noopener\">click()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">$(document).ready(function() {\n  \/\/ run when they click the \n\/ \/\/.accordion-header element\n  $('.accordion-header').click(function() {\n    \/\/ write code here for what happens on click\n    return false;\n});<\/code><\/pre>\n\n\n\n<p>Note, that when responding to clicks, the return false is there to prevent the page from reloading and scrolling back to the top.<\/p>\n\n\n\n<p>There are other event handlers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/api.jquery.com\/category\/events\/mouse-events\/\" target=\"_blank\">mouse event handlers<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/api.jquery.com\/category\/events\/keyboard-events\/\" target=\"_blank\">keyboard event handlers<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/api.jquery.com\/category\/events\/form-events\/\" target=\"_blank\">form event handlers<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jquery.com\/category\/events\/browser-events\/\" target=\"_blank\" rel=\"noreferrer noopener\">browser events<\/a> (like scrolling and resizing)<\/li>\n<\/ul>\n\n\n\n<p>and more.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Updating the DOM<\/h2>\n\n\n\n<p>There are a few ways you can update the dom.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add\/Remove\/Toggle Class<\/h3>\n\n\n\n<p>This is a common way to add interactivity. You can also take advantage of CSS styling, transition and animation. addClass() and removeClass() do what you expect.<\/p>\n\n\n\n<p>.<a href=\"https:\/\/api.jquery.com\/addClass\/\" target=\"_blank\" rel=\"noreferrer noopener\">addClass()<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/removeClass\/\" target=\"_blank\" rel=\"noreferrer noopener\">.removeClass()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').addClass('active');<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').removeClass('active');<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/toggleClass\/\" target=\"_blank\" rel=\"noreferrer noopener\">.toggleClass()<\/a><\/p>\n\n\n\n<p>Toggle class adds the class if it is not there and removes it if it is there.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').toggleClass('active');<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Show\/Hide\/Toggle Element<\/h3>\n\n\n\n<p>These three functions allow you to show and hide elements without removing them from the page.<\/p>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/hide\/\" target=\"_blank\" rel=\"noreferrer noopener\">.hide()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').hide();<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/show\/\" target=\"_blank\" rel=\"noreferrer noopener\">.show()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').show();<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/toggle\/\" target=\"_blank\" rel=\"noreferrer noopener\">.toggle()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').toggle();<\/code><\/pre>\n\n\n\n<p>Like above toggle hides if it&#8217;s showing and shows if it&#8217;s hidden.<\/p>\n\n\n\n<p>You can add a number of milliseconds (1000 = 1 second) to change how long the show and hide take.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">$('.myElement').show(400);\n<meta charset=\"utf-8\">$('.myElement').hide(400);\n$('.myElement').toggle(400);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add\/Remove Element<\/h3>\n\n\n\n<p>These functions allow you to dynamically add new elements to the page or remove existing elements. You can add the elements based on the user clicking, automatically or from data.<\/p>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/add\/\" target=\"_blank\" rel=\"noreferrer noopener\">.add()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').add(\"p\");<\/code><\/pre>\n\n\n\n<p>.<a href=\"https:\/\/api.jquery.com\/remove\/\" target=\"_blank\" rel=\"noreferrer noopener\">remove()<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('.myElement').remove();<\/code><\/pre>\n\n\n\n<p>There are more, these are just some of the basics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<p><a href=\"https:\/\/api.jquery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery Documentation<\/a>. This is the official documentation for jQuery.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.smashingmagazine.com\/2012\/05\/50-jquery-function-demos-for-aspiring-web-developers\/\" target=\"_blank\">50 Common j<\/a><a href=\"https:\/\/www.smashingmagazine.com\/2012\/05\/50-jquery-function-demos-for-aspiring-web-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\">Q<\/a><a rel=\"noreferrer noopener\" href=\"https:\/\/www.smashingmagazine.com\/2012\/05\/50-jquery-function-demos-for-aspiring-web-developers\/\" target=\"_blank\">uery functions<\/a>. This is more than you&#8217;ll need at first, but gives short explanations of what 50 of the most common functions do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced jQuery<\/h2>\n\n\n\n<p>jQuery can also be used to do things like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Animation<\/li>\n\n\n\n<li>Complex UI elements<\/li>\n\n\n\n<li>read and write data (AJAX)<\/li>\n<\/ul>\n\n\n\n<p>Those are not covered in this post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>jQuery is the most popular JavaScript Library. Using a library allows you to quickly write code that is compatible across a range of browsers. It should be noted that recently browsers and JavaScript have evolved to make it possible to do a lot of the things jQuery is good at without jQuery. You may see&hellip; <a class=\"more-link\" href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/topics\/javascript\/jquery-basics\/\">Continue reading <span class=\"screen-reader-text\">jQuery Basics<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","portfolio_post_id":0,"portfolio_citation":"","portfolio_annotation":"","openlab_post_visibility":"","footnotes":""},"categories":[20],"tags":[170,41,175],"coauthors":[87],"class_list":["post-2164","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-basics","tag-javascript","tag-jquery","entry"],"featured_image_urls_v2":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":"","post-thumbnail":"","gform-image-choice-sm":"","gform-image-choice-md":"","gform-image-choice-lg":""},"post_excerpt_stackable_v2":"<p>jQuery is the most popular JavaScript Library. Using a library allows you to quickly write code that is compatible across a range of browsers. It should be noted that recently browsers and JavaScript have evolved to make it possible to do a lot of the things jQuery is good at without jQuery. You may see posts and other writing recommending what is referred to as &#8220;vanilla&#8221; JavaScript. Check out the You Might not Need jQuery site to see a comparison. Learning vanilla JS is great, will help you do other things with JS and save your users downloading the jQuery&hellip;<\/p>\n","category_list_v2":"<a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/category\/topics\/javascript\/\" rel=\"category tag\">JavaScript<\/a>","author_info_v2":{"name":"Christopher Stein","url":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/author\/cstein\/"},"comments_num_v2":"1 comment","uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false,"gform-image-choice-sm":false,"gform-image-choice-md":false,"gform-image-choice-lg":false},"uagb_author_info":{"display_name":"Christopher Stein","author_link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/author\/cstein\/"},"uagb_comment_info":1,"uagb_excerpt":"jQuery is the most popular JavaScript Library. Using a library allows you to quickly write code that is compatible across a range of browsers. It should be noted that recently browsers and JavaScript have evolved to make it possible to do a lot of the things jQuery is good at without jQuery. You may see&hellip;&hellip;","_links":{"self":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/comments?post=2164"}],"version-history":[{"count":12,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2164\/revisions"}],"predecessor-version":[{"id":2903,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/posts\/2164\/revisions\/2903"}],"wp:attachment":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/media?parent=2164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/categories?post=2164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/tags?post=2164"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-spring23\/wp-json\/wp\/v2\/coauthors?post=2164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}