{"id":116,"date":"2022-09-17T16:51:06","date_gmt":"2022-09-17T16:51:06","guid":{"rendered":"https:\/\/mustafauzun.co\/blog\/?p=116"},"modified":"2022-09-17T16:56:39","modified_gmt":"2022-09-17T16:56:39","slug":"javascript-create-your-own-html-tag","status":"publish","type":"post","link":"https:\/\/mustafauzun.co\/blog\/javascript-create-your-own-html-tag\/","title":{"rendered":"Javascript, create your own HTML tag"},"content":{"rendered":"\n<p>Did you know that you can create your own HTML tags with JavaScript?<\/p>\n\n\n\n<p>You can use <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLElement\" data-type=\"URL\" data-id=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLElement\">HTMLElement<\/a> class with inheritance to do this.<\/p>\n\n\n\n<p>Here is an example;<\/p>\n\n\n\n<p>(Source: <a href=\"https:\/\/github.com\/mrna0\/lorem-ipsum.js\" data-type=\"URL\" data-id=\"https:\/\/github.com\/mrna0\/lorem-ipsum.js\">My Github<\/a>)<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>class LoremIpsum extends HTMLElement {\n  \n  constructor() {\n\n    super()\n\n  }\n\n  connectedCallback() {\n\n    let text = &#39;Lorem ipsum dolor sit amet, consectetur adipiscing elit.&#39; +\n                &#39;Vivamus semper id dolor et semper.&#39; +\n                &#39;In porttitor tellus non lacinia viverra.&#39; +\n                &#39;Mauris faucibus dui id gravida suscipit.&#39; +\n                &#39;Duis et nunc vitae justo vulputate varius.&#39; +\n                &#39;Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Sed nunc nisi, facilisis eget velit quis, auctor interdum erat.&#39; +\n                &#39;Aenean commodo odio odio, a tempus odio pulvinar in.&#39; +\n                &#39;Ut semper tellus nulla.&#39; +\n                &#39;Phasellus non nisl nisi.&#39; +\n                &#39;Fusce fermentum iaculis urna, sed dictum sem pulvinar et.&#39; +\n                &#39;Etiam ac sollicitudin eros, nec egestas dui. Donec euismod pretium nibh, ut laoreet turpis maximus vitae.&#39; +\n                &#39;Vestibulum purus neque, dignissim ac ligula ut, consequat porta nunc.&#39; +\n                &#39;Donec placerat porta turpis, in malesuada erat pulvinar vel.&#39;\n\n    if(this.hasAttribute(&#39;row&#39;)) {\n\n      text = text.repeat(this.getAttribute(&#39;row&#39;))\n\n    }\n\n    if(this.hasAttribute(&#39;length&#39;)) {\n\n      text = text.substr(0, this.getAttribute(&#39;length&#39;))\n\n    }\n\n    this.attachShadow({mode: &#39;open&#39;}).textContent = text\n\n  }\n\n}\n\nwindow.customElements.define(&#39;lorem-ipsum&#39;, LoremIpsum)<\/code><\/pre><\/div>\n\n\n\n<p>Usage;<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;lorem-ipsum row=&quot;1&quot;&gt;&lt;\/lorem-ipsum&gt;\n\n&lt;lorem-ipsum length=&quot;10&quot;&gt;&lt;\/lorem-ipsum&gt;\n&lt;!-- output: Lorem ipsu --&gt;<\/code><\/pre><\/div>\n\n\n\n<p>More; <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Web_Components\/Using_custom_elements\" data-type=\"URL\" data-id=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Web_Components\/Using_custom_elements\">MSDN<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that you can create your own HTML tags with JavaScript? You can use HTMLElement class with inheritance to do this. Here is<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,8],"tags":[34,9],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-html","category-javascript","tag-html","tag-javascript"],"_links":{"self":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":5,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":135,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions\/135"}],"wp:attachment":[{"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mustafauzun.co\/blog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}