{"id":28,"date":"2018-12-04T19:58:00","date_gmt":"2018-12-04T17:58:00","guid":{"rendered":"https:\/\/html-online.com\/articles\/?p=28"},"modified":"2020-01-14T10:39:04","modified_gmt":"2020-01-14T08:39:04","slug":"common-javascript-jquery-code-snippets","status":"publish","type":"post","link":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/","title":{"rendered":"The most common JavaScript and jQuery code snippets"},"content":{"rendered":"<p>I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn&#8217;t write a jQuery &#8220;document-ready&#8221; function&nbsp;on a blank piece of paper because my memory is not too sharp. This resource&nbsp;will help keeping these all together so we can always sneak a peek if necessary.<\/p>\n<p>Check out the interactive <a href=\"https:\/\/htmlcheatsheet.com\/js\/\" target=\"_blank\" rel=\"external nofollow noopener noreferrer\">JavaScript Cheat Sheet<\/a> to find them all in one place!<br \/>\n<!--more--><br \/>\n<strong>Show an element when another is clicked:<\/strong><\/p>\n<pre style=\"background: #fff; color: #3b3b3b;\"><span style=\"color: #069; font-weight: bold;\">$<\/span>(<span style=\"color: #a535ae;\">document<\/span>).ready(<span style=\"color: #ff5600;\">function<\/span> () {\n    <span style=\"color: #069; font-weight: bold;\">$<\/span>(<span style=\"color: #666;\">\"#button\"<\/span>).<span style=\"color: #45ae34; font-weight: bold;\">click<\/span>(<span style=\"color: #ff5600;\">function<\/span>(<span style=\"color: #a535ae;\">event<\/span>) {\n        <span style=\"color: #069; font-weight: bold;\">$<\/span>(<span style=\"color: #666;\">\"#box\"<\/span>).show();\n    });\n});\n<\/pre>\n<p><strong>Go through every instance of a class:<\/strong><\/p>\n<pre style=\"background: #fff; color: #3b3b3b;\"><span style=\"color: #069; font-weight: bold;\">$<\/span>(\"<span style=\"color: #666;\">.myclass\"<\/span>).each(<span style=\"color: #ff5600;\">function<\/span>() {\n    <span style=\"color: #21439c;\">console<\/span><span style=\"color: #45ae34; font-weight: bold;\">.log<\/span>(<span style=\"color: #069; font-weight: bold;\">$<\/span>(<span style=\"color: #0053ff; font-weight: bold;\">this<\/span>).html());\n});\n<\/pre>\n<p><strong>Get the content of an element by ID<\/strong><\/p>\n<pre style=\"background: #fff; color: #3b3b3b;\"><span style=\"color: #ff5600;\">var<\/span> x <span style=\"color: #069; font-weight: bold;\">=<\/span> <span style=\"color: #a535ae;\">document<\/span>.<span style=\"color: #45ae34; font-weight: bold;\">getElementById<\/span>(<span style=\"color: #666;\">\"demo\"<\/span>).innerHTML;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn&#8217;t write a jQuery &#8220;document-ready&#8221; function&nbsp;on a blank piece of paper because my memory is not too sharp. This resource&nbsp;will help keeping these all together so we can always sneak a peek if necessary. Check out &hellip; <a href=\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The most common JavaScript and jQuery code snippets&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,5,6],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-articles","category-javascript","category-jquery"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The most common JavaScript and jQuery code snippets<\/title>\n<meta name=\"description\" content=\"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The most common JavaScript and jQuery code snippets\" \/>\n<meta property=\"og:description\" content=\"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d\" \/>\n<meta property=\"og:url\" content=\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\" \/>\n<meta property=\"og:site_name\" content=\"HTML Online\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/htmlcoding\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-04T17:58:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-14T08:39:04+00:00\" \/>\n<meta name=\"author\" content=\"HTML Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"HTML Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\"},\"author\":{\"name\":\"HTML Editor\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\"},\"headline\":\"The most common JavaScript and jQuery code snippets\",\"datePublished\":\"2018-12-04T17:58:00+00:00\",\"dateModified\":\"2020-01-14T08:39:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\"},\"wordCount\":102,\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"articleSection\":[\"Articles\",\"JavaScript\",\"jQuery\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\",\"url\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\",\"name\":\"The most common JavaScript and jQuery code snippets\",\"isPartOf\":{\"@id\":\"https:\/\/html-online.com\/articles\/#website\"},\"datePublished\":\"2018-12-04T17:58:00+00:00\",\"dateModified\":\"2020-01-14T08:39:04+00:00\",\"description\":\"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d\",\"breadcrumb\":{\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/html-online.com\/articles\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The most common JavaScript and jQuery code snippets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/html-online.com\/articles\/#website\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"name\":\"HTML Online Articles\",\"description\":\"Tips, tricks, tutorials\u2026\",\"publisher\":{\"@id\":\"https:\/\/html-online.com\/articles\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/html-online.com\/articles\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/html-online.com\/articles\/#organization\",\"name\":\"HTML Online\",\"url\":\"https:\/\/html-online.com\/articles\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"contentUrl\":\"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg\",\"width\":350,\"height\":350,\"caption\":\"HTML Online\"},\"image\":{\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/htmlcoding\/\",\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d\",\"name\":\"HTML Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g\",\"caption\":\"HTML Editor\"},\"description\":\"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ferencdenes\/\",\"https:\/\/www.youtube.com\/@htmlg\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The most common JavaScript and jQuery code snippets","description":"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/","og_locale":"en_GB","og_type":"article","og_title":"The most common JavaScript and jQuery code snippets","og_description":"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d","og_url":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/","og_site_name":"HTML Online","article_publisher":"https:\/\/www.facebook.com\/htmlcoding\/","article_published_time":"2018-12-04T17:58:00+00:00","article_modified_time":"2020-01-14T08:39:04+00:00","author":"HTML Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"HTML Editor","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#article","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/"},"author":{"name":"HTML Editor","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d"},"headline":"The most common JavaScript and jQuery code snippets","datePublished":"2018-12-04T17:58:00+00:00","dateModified":"2020-01-14T08:39:04+00:00","mainEntityOfPage":{"@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/"},"wordCount":102,"publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"articleSection":["Articles","JavaScript","jQuery"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/","url":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/","name":"The most common JavaScript and jQuery code snippets","isPartOf":{"@id":"https:\/\/html-online.com\/articles\/#website"},"datePublished":"2018-12-04T17:58:00+00:00","dateModified":"2020-01-14T08:39:04+00:00","description":"I have collected the most common JavaScript and jQuery snippets that we use almost for every project. Honestly I couldn\u2019t write a jQuery \u201cdocument-ready\u201d","breadcrumb":{"@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/html-online.com\/articles\/common-javascript-jquery-code-snippets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/html-online.com\/articles\/"},{"@type":"ListItem","position":2,"name":"The most common JavaScript and jQuery code snippets"}]},{"@type":"WebSite","@id":"https:\/\/html-online.com\/articles\/#website","url":"https:\/\/html-online.com\/articles\/","name":"HTML Online Articles","description":"Tips, tricks, tutorials\u2026","publisher":{"@id":"https:\/\/html-online.com\/articles\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/html-online.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/html-online.com\/articles\/#organization","name":"HTML Online","url":"https:\/\/html-online.com\/articles\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/","url":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","contentUrl":"https:\/\/html-online.com\/articles\/wp-content\/uploads\/2022\/06\/logo.jpg","width":350,"height":350,"caption":"HTML Online"},"image":{"@id":"https:\/\/html-online.com\/articles\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/htmlcoding\/","https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/channel\/UCn38Jw1sJzbjVHO95Zp0Sww"]},{"@type":"Person","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/019f9afa07f209153df0fecfc90b8c1d","name":"HTML Editor","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/html-online.com\/articles\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7c1d8f5e7f1dc3e261766a96ac50c6a907fa5c236e87ab73379c57c9114e92cd?s=96&d=mm&r=g","caption":"HTML Editor"},"description":"In 2013, while wrestling with a mountain of client articles and an uncooperative CMS, I decided enough was enough. So, I created an online HTML editor purely out of necessity (and mild frustration). What began as a tool for my own sanity quickly evolved into a gift for the world\u2014or at least for anyone trying to avoid breaking their website's code. Since then, I've shared my tech notes on my blog, which serves as both a handy reference and a digital diary of the adventures and misadventures of a coder.","sameAs":["https:\/\/www.linkedin.com\/in\/ferencdenes\/","https:\/\/www.youtube.com\/@htmlg"]}]}},"_links":{"self":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":3,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":1139,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/posts\/28\/revisions\/1139"}],"wp:attachment":[{"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html-online.com\/articles\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}