{"id":94,"date":"2009-10-19T17:37:26","date_gmt":"2009-10-19T17:37:26","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2009\/10\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/"},"modified":"2009-10-19T17:37:26","modified_gmt":"2009-10-19T17:37:26","slug":"creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/","title":{"rendered":"Creating A New Element With jQuery, With Or Without A Plugin"},"content":{"rendered":"<p>\n<em>UPDATE: Whilst this remains an interesting example of creating a simple plugin, I no longer recommend this way of creating elements in jQuery. Instead just pass the html you want to use into jQuery directly as a string, e.g. <code>var myH1Element = jQuery(\"&lt;h1&gt;My H1 Test&lt;\/h1&gt;\");<\/code><\/em>.\n<\/p>\n<p>\nOne function that seems to be missing in <a href=\"http:\/\/www.jquery.com\/\">jQuery<\/a> is to create new elements outside of the current <acronym name=\"Document Object Model\">DOM<\/acronym>.\n<\/p>\n<p>\nThere is a good reason for this, normal vanilla JavaScript has the function createElement to do the job for you.\n<\/p>\n<p>\nTo create a new <code>h1<\/code> element for example, we can do the following.\n<\/p>\n<div class=\"code\"><code>var myH1Element = document.createElement('h1');<br \/>\n<\/code><\/div>\n<p>\nWe can easily combine this with jQuery to set the text inside for example&#8230;\n<\/p>\n<div class=\"code\"><code>jQuery(myH1Element).text(\"My H1 Text\");<br \/>\n<\/code><\/div>\n<p>\nThere is one downside, using the standard <code>createElement<\/code> we&#8217;re not able to chain functions with jQuery.\n<\/p>\n<p>\nThe way around this is to wrap the request in jQuery like this&#8230;\n<\/p>\n<div class=\"code\"><code>jQuery(document.createElement(\"h1\"));<br \/>\n<\/code><\/div>\n<p>\nNow we can chain this together as before&#8230;\n<\/p>\n<div class=\"code\"><code>jQuery(document.createElement(\"h1\")).text(\"My H1 Text\");<br \/>\n<\/code><\/div>\n<p>\nIf you don&#8217;t like the look of that, then we could always create a jQuery plugin, and add a <code>createElement<\/code> function to jQuery.\n<\/p>\n<div class=\"code\"><code>jQuery.extend({<br \/>\ncreateElement : function(elementName) {<br \/>\nreturn jQuery(document.createElement(elementName));<br \/>\n}<br \/>\n});<br \/>\n<\/code><\/div>\n<p>\nThat must be one of the simpliest jQuery plugins ever!\n<\/p>\n<p>\nNow we have extended jQuery with a <code>createElement<\/code> function we can chain calls to it like this.\n<\/p>\n<div class=\"code\"><code>jQuery.createElement(\"h1\").text(\"My H1 Text\").appendTo(\"body\");<br \/>\n<\/code><\/div>\n<p>\nThis will create a new <code>h1<\/code> element, add the text &#8220;My H1 Text&#8221; inside the <code>h1<\/code>, then append the <code>h1<\/code> inside the documents <code>body<\/code> tag.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: Whilst this remains an interesting example of creating a simple plugin, I no longer recommend this way of creating elements in jQuery. Instead just pass the html you want to use into jQuery directly as a string, e.g. var myH1Element = jQuery(&#8220;&lt;h1&gt;My H1 Test&lt;\/h1&gt;&#8221;);. One function that seems to be missing in jQuery is &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Creating A New Element With jQuery, With Or Without A Plugin&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[31,32,75],"class_list":["post-94","post","type-post","status-publish","format-standard","hentry","category-dev","tag-javascript","tag-jquery","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Creating A New Element With jQuery, With Or Without A Plugin - Robert Price<\/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:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating A New Element With jQuery, With Or Without A Plugin - Robert Price\" \/>\n<meta property=\"og:description\" content=\"UPDATE: Whilst this remains an interesting example of creating a simple plugin, I no longer recommend this way of creating elements in jQuery. Instead just pass the html you want to use into jQuery directly as a string, e.g. var myH1Element = jQuery(&quot;&lt;h1&gt;My H1 Test&lt;\/h1&gt;&quot;);. One function that seems to be missing in jQuery is &hellip; Continue reading &quot;Creating A New Element With jQuery, With Or Without A Plugin&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2009-10-19T17:37:26+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\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:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Creating A New Element With jQuery, With Or Without A Plugin\",\"datePublished\":\"2009-10-19T17:37:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/\"},\"wordCount\":223,\"keywords\":[\"JavaScript\",\"jQuery\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/\",\"name\":\"Creating A New Element With jQuery, With Or Without A Plugin - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2009-10-19T17:37:26+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating A New Element With jQuery, With Or Without A Plugin\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Creating A New Element With jQuery, With Or Without A Plugin - Robert Price","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:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Creating A New Element With jQuery, With Or Without A Plugin - Robert Price","og_description":"UPDATE: Whilst this remains an interesting example of creating a simple plugin, I no longer recommend this way of creating elements in jQuery. Instead just pass the html you want to use into jQuery directly as a string, e.g. var myH1Element = jQuery(\"&lt;h1&gt;My H1 Test&lt;\/h1&gt;\");. One function that seems to be missing in jQuery is &hellip; Continue reading \"Creating A New Element With jQuery, With Or Without A Plugin\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/","og_site_name":"Robert Price","article_published_time":"2009-10-19T17:37:26+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Creating A New Element With jQuery, With Or Without A Plugin","datePublished":"2009-10-19T17:37:26+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/"},"wordCount":223,"keywords":["JavaScript","jQuery","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/","name":"Creating A New Element With jQuery, With Or Without A Plugin - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2009-10-19T17:37:26+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/creating_a_new_element_with_jquery_with_or_without_a_plugin-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Creating A New Element With jQuery, With Or Without A Plugin"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}