{"id":615,"date":"2023-02-24T08:11:07","date_gmt":"2023-02-24T08:11:07","guid":{"rendered":"https:\/\/thoth.dk\/?page_id=615"},"modified":"2023-02-25T11:21:30","modified_gmt":"2023-02-25T11:21:30","slug":"jquery-and-wordpress","status":"publish","type":"page","link":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/","title":{"rendered":"jQuery and WordPress"},"content":{"rendered":"\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6aafeceacf887&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6aafeceacf887\" class=\"wp-block-image aligncenter size-full has-custom-border wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"512\" height=\"512\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg\" alt=\"\" class=\"wp-image-628\" style=\"border-radius:44px\" srcset=\"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg 512w, https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder-300x300.jpeg 300w, https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder-150x150.jpeg 150w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/stablediffusionweb.com\/#demo\">Stable Difusion:<\/a> A superwoman writing jQuery code at a computer.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><a rel=\"noreferrer noopener\" href=\"https:\/\/jquery.com\/\" target=\"_blank\">jQuery<\/a> is a very popular JavaScript <a href=\"https:\/\/kinsta.com\/blog\/javascript-libraries\/\">library<\/a> for WordPress. It&#8217;s easy to use for those of us who know CSS. For some programmers the library will open the gates of creative superpowers. More conservative programmers don&#8217;t like it at all. Here are some pros and cons.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Discussion: should I use jQuery or not?<\/h3>\n\n\n\n<figure class=\"wp-block-table alignwide is-style-stripes\"><table class=\"has-pale-cyan-blue-background-color has-background has-fixed-layout\"><thead><tr><th>Pro<\/th><th>Con<\/th><\/tr><\/thead><tbody><tr><td>The jQuery library is used by many companies worldwide. Knowing it is a competence. Ease of use, the selector is:<br><em>$(&#8216;beer&#8217;)<\/em><\/td><td>Why not use the standard selector and JavaScript? Like;<br><em>document.getElementById(&#8216;#beer&#8217;)<\/em><\/td><\/tr><tr><td>Shorter codelines inspired by CSS. If you&#8217;re used to CSS it&#8217;s very easy to select classes and tags. For instance you don&#8217;t have to loop out the members of a class or an array of tags.<\/td><td>Concise codelines, you know what they do. Why invent an alternative way of doing things? That will burden the brain. A loop is hardly a problem for a serious programmer.<\/td><\/tr><tr><td>Because jQuery is widely used the library is probably loaded in the browser&#8217;s cache anyway<\/td><td>The browser must load a somewhat bloated library, and often several libraries. (Bloated = too many codelines).<\/td><\/tr><tr><td>There are thousands of plugins that will ease the development of UI-ideas. You can get these plugins via the <a href=\"https:\/\/plugins.jquery.com\/\">Plugin Registry<\/a> or via the <a href=\"https:\/\/nodejs.org\/en\/\">Nodejs npm<\/a> or alternative Nodejs packet managers such as <a href=\"https:\/\/bower.io\/\">Bower<\/a>.<\/td><td>Write your own code! That&#8217;s the hallmark of a good programmer.<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">The discussion: is jQuery a good or bad practise?<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Add jQuery Power to Your Theme<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can add jQuery by editing the functions.php file, however that&#8217;s not necessary. I&#8217;d recommend that you install jQuery viaCustom CSS &amp; Js plugin. Add the CDN as a <strong>HTML block<\/strong> in the header section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- Important: you must add this code by \"Add HTML code\" --&gt;\n\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.3.min.js\" integrity=\"sha256-pvPw+upLPUjgMXY0G+8O0xUf+\/Im1MZjXxxgOcBQBXU=\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Get Recent CDN versions:<\/strong><br>You&#8217;ll <a href=\"https:\/\/releases.jquery.com\/\">find the most recent CDN here<\/a>. Go for the most recent and stable core version. In production you&#8217;ll probably want a minified version of jQuery (in order to reduce load time).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to Use jQuery<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Are the page elements loaded?<\/strong><br>jQuery has a control method that will check whether all the DOM elements are loaded by the browser. Normally you sandwich jQuery code in such a statement. Of course you only need to do this once. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(document).ready(function(){\n\n  \/\/ add your jQuery code here ...\n\n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can add statements to the code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$(document).ready(function(){\n\n  $(\"#myBtn\").click(function(){\n    $(\"p\").hide( 300 );\n    $(\"#red\").toggle( 400 );\n  });\n\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">jQuery Event Handling<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The statement above is a typical event handler in jQuery. When you create the UI the user should click a button, an image or similar &#8211; and then the functions is fired off. In this case a toggle and hide animation is used. But how do we add this code to WordPress?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Watch this tutorial:<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\" id=\"myBtn\"><a class=\"wp-block-button__link wp-element-button\">Toggle &lt;p&gt; elements<\/a><\/div>\n<\/div>\n\n\n\n<script>\n $(document).ready(function(){\n\n  $(\"#myBtn\").click(function(){\n    $(\"p\").toggle( 300 );\n  });\n\n});\n<\/script>\n\n\n\n<h4 class=\"wp-block-heading\">Exercise: A jQuery Button<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Now create a jQuery button. <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Create a jQuery Toggle Button in WordPress 6.++\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/fpkZl_15JrQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Tutorials<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>W3 Schools: <a href=\"https:\/\/www.w3schools.com\/jquery\/\">jQuery Tutorial<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/jquery\/\" target=\"_blank\" rel=\"noreferrer noopener\">GeeksForGeeks &#8211; jQuery<\/a> (and a list of tutorials towards the bottom of the page)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">jQuery UI<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Several UI elements, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accordions<\/li>\n\n\n\n<li>Effects &#8211; add \/ remove classes<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Plugins<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are several hundred plugins for jQuery:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/plugins.jquery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/plugins.jquery.com\/<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Links<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.jquery.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery API<\/a><\/li>\n\n\n\n<li><a rel=\"noreferrer noopener\" href=\"https:\/\/api.jqueryui.com\/\" target=\"_blank\">jQuery UI API Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jqueryui.com\/effect\/\" target=\"_blank\" rel=\"noreferrer noopener\">jQuery Effects<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jqueryui.com\/category\/widgets\/\">jQuery Widgets<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/api.jquery.com\/animate\/\" target=\"_blank\" rel=\"noreferrer noopener\">.animate<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>jQuery is a very popular JavaScript library for WordPress. It&#8217;s easy to use for those of us who know CSS. For some programmers the library will open the gates of creative superpowers. More conservative programmers don&#8217;t like it at all. Here are some pros and cons. Discussion: should I use jQuery or not? Pro Con [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"wp-custom-template-international-ux","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-615","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>jQuery and WordPress - Wordpress og WooCommerce<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"da_DK\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"jQuery and WordPress - Wordpress og WooCommerce\" \/>\n<meta property=\"og:description\" content=\"jQuery is a very popular JavaScript library for WordPress. It&#8217;s easy to use for those of us who know CSS. For some programmers the library will open the gates of creative superpowers. More conservative programmers don&#8217;t like it at all. Here are some pros and cons. Discussion: should I use jQuery or not? Pro Con [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"Wordpress og WooCommerce\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-25T11:21:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimeret l\u00e6setid\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutter\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/\",\"url\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/\",\"name\":\"jQuery and WordPress - Wordpress og WooCommerce\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thoth.dk\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thoth.dk\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/superwoman-coder.jpeg\",\"datePublished\":\"2023-02-24T08:11:07+00:00\",\"dateModified\":\"2023-02-25T11:21:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/#breadcrumb\"},\"inLanguage\":\"da-DK\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"da-DK\",\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thoth.dk\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/superwoman-coder.jpeg\",\"contentUrl\":\"https:\\\/\\\/thoth.dk\\\/wp-content\\\/uploads\\\/2023\\\/02\\\/superwoman-coder.jpeg\",\"width\":512,\"height\":512,\"caption\":\"Super Girl\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thoth.dk\\\/index.php\\\/jquery-and-wordpress\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thoth.dk\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"jQuery and WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thoth.dk\\\/#website\",\"url\":\"https:\\\/\\\/thoth.dk\\\/\",\"name\":\"Wordpress og WooCommerce\",\"description\":\"Syv kreative dage med WordPress og WooCommerce\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thoth.dk\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"da-DK\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"jQuery and WordPress - Wordpress og WooCommerce","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:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/","og_locale":"da_DK","og_type":"article","og_title":"jQuery and WordPress - Wordpress og WooCommerce","og_description":"jQuery is a very popular JavaScript library for WordPress. It&#8217;s easy to use for those of us who know CSS. For some programmers the library will open the gates of creative superpowers. More conservative programmers don&#8217;t like it at all. Here are some pros and cons. Discussion: should I use jQuery or not? Pro Con [&hellip;]","og_url":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/","og_site_name":"Wordpress og WooCommerce","article_modified_time":"2023-02-25T11:21:30+00:00","og_image":[{"width":512,"height":512,"url":"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimeret l\u00e6setid":"3 minutter"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/","url":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/","name":"jQuery and WordPress - Wordpress og WooCommerce","isPartOf":{"@id":"https:\/\/thoth.dk\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/#primaryimage"},"image":{"@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/#primaryimage"},"thumbnailUrl":"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg","datePublished":"2023-02-24T08:11:07+00:00","dateModified":"2023-02-25T11:21:30+00:00","breadcrumb":{"@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/#breadcrumb"},"inLanguage":"da-DK","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/"]}]},{"@type":"ImageObject","inLanguage":"da-DK","@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/#primaryimage","url":"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg","contentUrl":"https:\/\/thoth.dk\/wp-content\/uploads\/2023\/02\/superwoman-coder.jpeg","width":512,"height":512,"caption":"Super Girl"},{"@type":"BreadcrumbList","@id":"https:\/\/thoth.dk\/index.php\/jquery-and-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thoth.dk\/"},{"@type":"ListItem","position":2,"name":"jQuery and WordPress"}]},{"@type":"WebSite","@id":"https:\/\/thoth.dk\/#website","url":"https:\/\/thoth.dk\/","name":"Wordpress og WooCommerce","description":"Syv kreative dage med WordPress og WooCommerce","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thoth.dk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"da-DK"}]}},"_links":{"self":[{"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/pages\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":36,"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/pages\/615\/revisions"}],"predecessor-version":[{"id":702,"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/pages\/615\/revisions\/702"}],"wp:attachment":[{"href":"https:\/\/thoth.dk\/index.php\/wp-json\/wp\/v2\/media?parent=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}