{"id":28165,"date":"2016-10-10T21:01:03","date_gmt":"2016-10-10T13:01:03","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=28165"},"modified":"2023-04-06T19:20:39","modified_gmt":"2023-04-06T11:20:39","slug":"dom-manipulation-javascript-methods","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/","title":{"rendered":"Master DOM Manipulation with 15 Essential JavaScript Methods"},"content":{"rendered":"<p>As a web developer, you frequently need to manipulate the <abbr title=\"Document Object Model\">DOM<\/abbr>, the object model that is used by browsers to <strong>specify the logical structure<\/strong> of web pages, and based on this structure to <strong>render HTML elements on the screen<\/strong>.<\/p>\n<p>HTML defines the <strong>default DOM structure<\/strong>. However in many cases you may want to manipulate this with JavaScript, usually in order to <strong>add extra functionalities<\/strong> to a site.<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg\" width=\"700\" height=\"301\" alt=\"DOM Tree\"><figcaption><a href=\"https:\/\/web.dev\/\/performance\/critical-rendering-path\/constructing-the-object-model?hl=en\" target=\"_blank\" rel=\"noopener nofollow\">IMAGE: Google Developers<\/a><\/figcaption><\/figure>\n<p>In this post, you will find a list of <strong>15 basic JavaScript methods<\/strong> that <strong>aid DOM manipulation<\/strong>. You\u2019d likely  use these methods frequently in your code, and you will also  bump into them in our JavaScript tutorials.<\/p>\n<p class=\"recommended_top\">\n\t\t\t\t\t<strong>Read Also:<\/strong>\u00a0\n\t\t\t\t\t<a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/useful-javascript-statements\/\">4 Useful JavaScript Statements You Should Know<\/a>\n\t\t\t\t<\/p>\n<h2>1. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelector\" target=\"_blank\" rel=\"noopener\"><code>querySelector()<\/code><\/a><\/h2>\n<p>The <code>querySelector()<\/code> method <strong>returns the first element that matches one or more CSS selectors<\/strong>. If no match is found, it returns <code>null<\/code>.<\/p>\n<p>Before <code>querySelector()<\/code> was introduced, developers widely used the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/getElementById\" target=\"_blank\" rel=\"noopener\"><code>getElementById()<\/code><\/a> method which <strong>fetches an element with a specified <code>id<\/code> value<\/strong>.<\/p>\n<p>Although <code>getElementById()<\/code> is still a useful method, but with the newer <code>querySelector()<\/code> and <code>querySelectorAll()<\/code> methods we are free to <strong>target elements based on any <a href=\"https:\/\/www.w3.org\/TR\/css3-selectors\/#selectors\" target=\"_blank\" rel=\"noopener nofollow\">CSS selector<\/a><\/strong>, thus we have more flexibility.<\/p>\n<h3>Syntax<\/h3>\n<pre>var ele = document.querySelector(selector);<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 First matching element or <code>null<\/code> (if no element matches the selectors)<\/li>\n<li><code>selector<\/code> \u2013 one or more CSS selectors, such as <code>\"#fooId\"<\/code>, <code>\".fooClassName\"<\/code>, <code>\".class1.class2\"<\/code>, or <code>\".class1, .class2\"<\/code><\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, the first <code>&lt;div&gt;<\/code> gets selected with the <code>querySelector()<\/code> method and its color is changed to red.<\/p>\n<pre>\r\n&lt;p&gt;paragraph one&lt;\/p&gt;\r\n&lt;p&gt;paragraph two&lt;\/p&gt;\r\n&lt;div&gt;div one&lt;\/div&gt;\r\n&lt;p&gt;paragraph three&lt;\/p&gt;\r\n&lt;div&gt;div two&lt;\/div&gt;\r\n<\/pre>\n<pre>\r\nvar firstDiv = document.querySelector('div');\r\nfirstDiv.style.color = 'red';\r\n<\/pre>\n<h3>Interactive Demo<\/h3>\n<p>Test the <code>querySelector()<\/code> method in the following interactive demo. Just type a selector matching the ones you can find inside the blue boxes (e.g. <code>#three<\/code>), and click the <span class=\"key\">Select<\/span> button. Note that if you type <code>.block<\/code>, <strong>only its first instance<\/strong> will be selected.<\/p>\n<p><iframe height=\"420\" scrolling=\"no\" src=\"https:\/\/codepen.io\/\/hkdc\/embed\/amVZBz\/?height=420&theme-id=0&default-tab=result&embed-version=2\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" style=\"width: 100%;\">See the Pen <a href=\"https:\/\/codepen.io\/\/hkdc\/pen\/amVZBz\/\" rel=\"nofollow\">JS \u2013 querySelector<\/a> by HONGKIAT (<a href=\"https:\/\/codepen.io\/\/hkdc\" rel=\"nofollow\">@hkdc<\/a>) on <a href=\"https:\/\/codepen.io\/\" rel=\"nofollow\">CodePen<\/a>. <\/iframe><\/p>\n<h2>2. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/querySelectorAll\" target=\"_blank\" rel=\"noopener\"><code>querySelectorAll()<\/code><\/a><\/h2>\n<p>Unlike <code>querySelector()<\/code> that returns only the first instance of all matching elements, <code>querySelectorAll()<\/code> <strong>returns all elements that match the specified CSS selector<\/strong>.<\/p>\n<p>The matching elements are returned as a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/NodeList\" target=\"_blank\" rel=\"noopener\"><code>NodeList<\/code><\/a> object that will be an empty object if no matching elements are found.<\/p>\n<h3>Syntax<\/h3>\n<pre>var eles = document.querySelectorAll(selector);<\/pre>\n<ul>\n<li><code>eles<\/code> \u2013 A <code>NodeList<\/code> object with all matching elements as property values. The object is empty if no matches are found.<\/li>\n<li><code>selector<\/code> \u2013 one or more CSS selectors, such as <code>\"#fooId\"<\/code>, <code>\".fooClassName\"<\/code>, <code>\".class1.class2\"<\/code>, or <code>\".class1, .class2\"<\/code><\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>The example  below uses the same HTML as the previous one. However, in this example, all paragraphs are selected with <code>querySelectorAll()<\/code>, and are colored blue.<\/p>\n<pre>\r\n&lt;p&gt;paragraph one&lt;\/p&gt;\r\n&lt;p&gt;paragraph two&lt;\/p&gt;\r\n&lt;div&gt;div one&lt;\/div&gt;\r\n&lt;p&gt;paragraph three&lt;\/p&gt;\r\n&lt;div&gt;div two&lt;\/div&gt;\r\n<\/pre>\n<pre>\r\nvar paragraphs = document.querySelectorAll('p');\r\nfor(var p of paragraphs)\r\n\tp.style.color = 'blue';\r\n<\/pre>\n<h2>3. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/EventTarget\/addEventListener\" target=\"_blank\" rel=\"noopener\"><code>addEventListener()<\/code><\/a><\/h2>\n<p><strong>Events<\/strong> refer to what happens to an HTML element, such as clicking, focusing, or loading, to which we can react with JavaScript. We can assign JS functions to <em>listen<\/em> for these events in elements and do something when the event had occured.<\/p>\n<p>There are three ways you can <strong>assign a function to a certain event.<\/strong><\/p>\n<p>If <code>foo()<\/code> is a custom function, you can register it <strong>as a click event listener<\/strong> (call it when the button element is clicked) in three ways:<\/p>\n<ol>\n<li>\n<pre>\r\n&lt;button onclick=foo&gt;Alert&lt;\/button&gt;<\/pre>\n<\/li>\n<li>\n<pre>\r\nvar btn = document.querySelector('button');\r\nbtn.onclick=foo;<\/pre>\n<\/li>\n<li>\n<pre>\r\nvar btn = document.querySelector('button');\r\nbtn.addEventListener('click', foo);<\/pre>\n<\/li>\n<\/ol>\n<p>The method <code>addEventListener()<\/code> (the third solution) has <strong>some pros<\/strong>; it is the latest standard \u2013 allowing the assignment of more than one function as event listeners to one event \u2013 and comes with a useful set of options.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.addEventListener(evt, listener, [options]);<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 The HTML element the event listener will be listening for.<\/li>\n<li><code>evt<\/code> \u2013 The targeted event.<\/li>\n<li><code>listener<\/code> \u2013 Typically, a JavaScript function.<\/li>\n<li><code>options<\/code> \u2013 (optional) An object with a set of boolean properties (listed below).<\/li>\n<\/ul>\n<table>\n<thead>\n<tr>\n<td>Options<\/td>\n<td>What happens, when it is set to <code>true<\/code>?<\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>capture<\/code><\/td>\n<td>\n<p>Stops event bubbling, i.e. prevents calling of any event listeners for the same event type in the element\u2019s ancestors.<\/p>\n<p>To use this feature, you can use 2 syntaxes:<\/p>\n<ol>\n<li><code>ele.addEventListener(evt, listener, true)<\/code><\/li>\n<li><code>ele.addEventListener(evt, listener, {capture:true});<\/code><\/li>\n<\/ol>\n<\/td>\n<\/tr>\n<tr>\n<td><code>once<\/code><\/td>\n<td>\n<p>Listener is called only the first time the event happens, then it is automatically detached from the event, and won\u2019t be triggered  by it anymore.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><code>passive<\/code><\/td>\n<td>\n<p>The default action of the event cannot be stopped with the <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/Event\/preventDefault\" target=\"_blank\" rel=\"noopener\">preventDefault()<\/a> method.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Code Example<\/h3>\n<p>In this example, we add a click event listener called <code>foo<\/code>, to the <code>&lt;button&gt;<\/code> HTML tag.<\/p>\n<pre>\r\n&lt;button&gt;Click Me&lt;\/button&gt;<\/pre>\n<pre>\r\nvar btn = document.querySelector('button');\r\nbtn.addEventListener('click',foo);\r\nfunction foo() { alert('hello'); }<\/pre>\n<h3>Interactive Demo<\/h3>\n<p>Assign the <code>foo()<\/code> custom function as an event listener to any of the three following events: <code>input<\/code>, <code>click<\/code> or <code>mouseover<\/code> & trigger the chosen event in the bottom input field by hovering, clicking or typing in it.<\/p>\n<p><iframe height=\"400\" scrolling=\"no\" src=\"https:\/\/codepen.io\/\/hkdc\/embed\/rrYXWR\/?height=400&theme-id=0&default-tab=result&embed-version=2\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" style=\"width: 100%;\">See the Pen <a href=\"https:\/\/codepen.io\/\/hkdc\/pen\/rrYXWR\/\" rel=\"nofollow\">JS \u2013 addEventListener<\/a> by HONGKIAT (<a href=\"https:\/\/codepen.io\/\/hkdc\" rel=\"nofollow\">@hkdc<\/a>) on <a href=\"https:\/\/codepen.io\/\" rel=\"nofollow\">CodePen<\/a>. <\/iframe><\/p>\n<h2>4. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/EventTarget\/removeEventListener\" target=\"_blank\" rel=\"noopener\"><code>removeEventListener()<\/code><\/a><\/h2>\n<p>The <code>removeEventListener()<\/code> method <strong>detaches an event listener<\/strong> previously added with the <code>addEventListener()<\/code> method <strong>from the event it is listening for<\/strong>.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.removeEventListener(evt, listener, [options]);<\/pre>\n<p>Uses the same syntax as the aforementioned <code>addEventListener()<\/code> method (except for the <code>once<\/code> option that\u2019s excluded). The options are used to be very specific about identifying the listener to be detached.<\/p>\n<h3>Code Example<\/h3>\n<p>Following the Code Example we used at <code>addEventListener()<\/code>, here we remove the click event listener called <code>foo<\/code> from the <code>&lt;button&gt;<\/code> element.<\/p>\n<pre>\r\nbtn.removeEventListener('click',foo);<\/pre>\n<h2>5. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/createElement\" target=\"_blank\" rel=\"noopener\"><code>createElement()<\/code><\/a><\/h2>\n<p>The <code>createElement()<\/code> method <strong>creates a new HTML element<\/strong> using the <strong>name of the HTML tag<\/strong> to be created, such as <code>'p'<\/code> or <code>'div'<\/code>.<\/p>\n<p>You can later add this element to the web page by using different <strong>methods for DOM insertion<\/strong>, such as <code>AppendChild()<\/code> (see later in this post).<\/p>\n<h3>Syntax<\/h3>\n<pre>document.createElement(tagName);<\/pre>\n<ul>\n<li><code>tagName<\/code> \u2013 The name of the HTML tag you want to create.\n<\/li><\/ul>\n<h3>Code Example<\/h3>\n<p>With the following example, you can create a new paragraph element:<\/p>\n<pre>var pEle = document.createElement('p')<\/pre>\n<h2>6. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/appendChild\" target=\"_blank\" rel=\"noopener\"><code>appendChild()<\/code><\/a><\/h2>\n<p>The <code>appendChild()<\/code> method <strong>adds an element as the last child<\/strong> to the HTML element that invokes this method.<\/p>\n<p>The child to be inserted can be either a <strong>newly created element<\/strong>, or an <strong>already existing one<\/strong>. In the latter case, it will be moved from its previous position to the position of the last child.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.appendChild(childEle)<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 The HTML element to which <code>childEle<\/code> is added as its last child.<\/li>\n<li><code>childEle<\/code> \u2013 The HTML element added as the last child of <code>ele<\/code>.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we insert a <code>&lt;strong&gt;<\/code> element is as the child of a <code>&lt;div&gt;<\/code> element using the <code>appendChild()<\/code> and the aforementioned <code>createElement()<\/code> methods.<\/p>\n<pre>&lt;div&gt;&lt;\/div&gt;<\/pre>\n<pre>\r\nvar div = document.querySelector('div');\r\nvar strong = document.createElement('strong');\r\nstrong.textContent = 'Hello';\r\ndiv.appendChild(strong);<\/pre>\n<h3>Interactive Demo<\/h3>\n<p>In the following interactive demo, letters from <code>#a<\/code> to <code>#r<\/code> are the child elements of the <code>#parent-one<\/code>, <code>#parent-two<\/code>, and <code>#parent-three<\/code> id selectors.<\/p>\n<p>Check out how the <code>appendChild()<\/code> method works by <strong>typing one parent and one child selector name into the input fields below. <\/strong>You can choose children belonging to another parent as well.<\/p>\n<p><iframe height=\"680\" scrolling=\"no\" src=\"https:\/\/codepen.io\/\/hkdc\/embed\/QKOZvk\/?height=680&theme-id=0&default-tab=result&embed-version=2\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" style=\"width: 100%;\">See the Pen <a href=\"https:\/\/codepen.io\/\/hkdc\/pen\/QKOZvk\/\" rel=\"nofollow\">JS \u2013 appendChild<\/a> by HONGKIAT (<a href=\"https:\/\/codepen.io\/\/hkdc\" rel=\"nofollow\">@hkdc<\/a>) on <a href=\"https:\/\/codepen.io\/\" rel=\"nofollow\">CodePen<\/a>. <\/iframe><\/p>\n<h2>7. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/removeChild\" target=\"_blank\" rel=\"noopener\"><code>removeChild()<\/code><\/a><\/h2>\n<p>The <code>removeChild()<\/code> method <strong>removes a specified child element<\/strong> from the HTML element that calls this method.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.removeChild(childEle)<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 The parent element of <code>childEle<\/code>.<\/li>\n<li><code>childEle<\/code> \u2013 The child element of <code>ele<\/code>.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we remove the <code>&lt;strong&gt;<\/code> element we added as a child to the <code>&lt;div&gt;<\/code> tag at the Code Example for the previous <code>appendChild()<\/code> method.<\/p>\n<pre>div.removeChild(strong);<\/pre>\n<h2>8. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/replaceChild\" target=\"_blank\" rel=\"noopener\"><code>replaceChild()<\/code><\/a><\/h2>\n<p>The <code>replaceChild()<\/code> method <strong>replaces a child element with another one<\/strong> belonging to the parent element that calls this method.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.replaceChild(newChildEle, oldChileEle)<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 Parent element of which children are to be replaced.<\/li>\n<li><code>newChildEle<\/code> \u2013 Child element of <code>ele<\/code> that will replace <code>oldChildEle<\/code>.<\/li>\n<li><code>oldChildEle<\/code> \u2013 Child element of <code>ele<\/code>, that will be replaced by <code>newChildEle<\/code>.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example the child element <code>&lt;strong&gt;<\/code> belonging to the <code>&lt;div&gt;<\/code> parent element is replaced with a newly created <code>&lt;em&gt;<\/code> tag.<\/p>\n<pre>\r\n&lt;div&gt;\r\n  &lt;strong&gt;hello&lt;\/strong&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre>\r\nvar em = document.createElement('em');\r\nvar strong = document.querySelector('strong');\r\nvar div = document.querySelector('div');\r\nem.textContent = 'hi';\r\ndiv.replaceChild(em, strong);<\/pre>\n<h2>9. <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/Node\/cloneNode\" target=\"_blank\" rel=\"noopener\"><code>cloneNode()<\/code><\/a><\/h2>\n<p>When you have to create a new element that needs to be the <strong>same as an already existing element<\/strong> on the web page, you can simply <strong>create a copy of the already existing element<\/strong> using the <code>cloneNode()<\/code> method.<\/p>\n<h3>Syntax<\/h3>\n<p><code>var dupeEle = ele.cloneNode([deep])<\/code><\/p>\n<ul>\n<li><code>dupeEle<\/code> \u2013 Copy of the <code>ele<\/code> element.<\/li>\n<li><code>ele<\/code> \u2013 The HTML element to be copied.<\/li>\n<li><code>deep<\/code> \u2013 (optional) A boolean value. If it\u2019s set to <code>true<\/code>, <code>dupeEle<\/code> will have all the child elements <code>ele<\/code> has, if it\u2019s set to <code>false<\/code> it will be cloned without its children.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we create a copy for the <code>&lt;strong&gt;<\/code> element with <code>cloneNode()<\/code>, then we add it to the <code>&lt;div&gt;<\/code> tag as a child element with the aforementioned <code>appendChild()<\/code> method.<\/p>\n<p>As a result, <code>&lt;div&gt;<\/code> will contain two <code>&lt;strong&gt;<\/code> elements, both with the <code>hello<\/code> string as content.<\/p>\n<pre>\r\n&lt;div&gt;\r\n  &lt;strong&gt;hello&lt;\/strong&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre>\r\nvar strong = document.querySelector('strong');\r\nvar copy = strong.cloneNode(true);\r\nvar div = document.querySelector('div');\r\ndiv.appendChild(copy);\r\n<\/pre>\n<h2>10. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Node\/insertBefore\" target=\"_blank\" rel=\"noopener\"><code>insertBefore()<\/code><\/a><\/h2>\n<p>The <code>insertBefore()<\/code> method <strong>adds a specified child element before another child element<\/strong>. The method is called by the parent element.<\/p>\n<p>If the referenced child element does not exist or you pass explicitly <code>null<\/code> in its place, the child element to be inserted is added <strong>as the last child of the parent<\/strong> (similar to <code>appendChild()<\/code>).<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.insertBefore(newEle, refEle);<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 Parent element.<\/li>\n<li><code>newEle<\/code> \u2013 New HTML element to be inserted.<\/li>\n<li><code>refEle<\/code> \u2013 A child element of <code>ele<\/code> <strong>before which<\/strong> <code>newEle<\/code> will be inserted.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we create a new <code>&lt;em&gt;<\/code> element with some text inside, and add it <strong>before<\/strong> the <code>&lt;strong&gt;<\/code> element inside the <code>&lt;div&gt;<\/code> parent element.<\/p>\n<pre>\r\n&lt;div&gt;\r\n  &lt;strong&gt;hello&lt;\/strong&gt;\r\n&lt;\/div&gt;<\/pre>\n<pre>\r\nvar em = document.createElement('em');\r\nvar strong = document.querySelector('strong');\r\nvar div = document.querySelector('div');\r\nem.textContent = 'hi';\r\ndiv.insertBefore(em, strong);\r\n<\/pre>\n<h3>Interactive Demo<\/h3>\n<p>This interactive demo works similarly to our previous demo belonging to the <code>appendChild()<\/code> method. Here you only need to type the id selectors of two child elements (from <code>#a<\/code> to <code>#r<\/code>) into the input boxes, and you can see how the <code>insertBefore()<\/code> method moves the first specified child <strong>before<\/strong> the second.<\/p>\n<p><iframe height=\"680\" scrolling=\"no\" src=\"https:\/\/codepen.io\/\/hkdc\/embed\/xEPvLw\/?height=680&theme-id=0&default-tab=result&embed-version=2\" frameborder=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" style=\"width: 100%;\">See the Pen <a href=\"https:\/\/codepen.io\/\/hkdc\/pen\/xEPvLw\/\" rel=\"nofollow\">JS \u2013 insertBefore<\/a> by HONGKIAT (<a href=\"https:\/\/codepen.io\/\/hkdc\" rel=\"nofollow\">@hkdc<\/a>) on <a href=\"https:\/\/codepen.io\/\" rel=\"nofollow\">CodePen<\/a>. <\/iframe><\/p>\n<h2>11. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Document\/createDocumentFragment\" target=\"_blank\" rel=\"noopener\"><code>createDocumentFragment()<\/code><\/a><\/h2>\n<p>The <code>createDocumentFragment()<\/code> method may not be as well known as the others in this list, nevertheless is an important one, especially if you want to <strong>insert multiple elements in bulk<\/strong>, such as adding multiple rows to a table.<\/p>\n<p>It <strong>creates a <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/DocumentFragment\" target=\"_blank\" rel=\"noopener\"><code>DocumentFragment<\/code><\/a> object<\/strong>, which essentially is <strong>a DOM node that is not part of the DOM tree<\/strong>. It\u2019s like a buffer where we can add and store other elements (e.g. multiple rows) first, before adding them to the desired node in the DOM tree (e.g. to a table).<\/p>\n<p>Why don\u2019t we just add elements directly to the DOM tree? Because DOM insertion <strong>causes layout changes<\/strong>, and it\u2019s an <strong>expensive browser process<\/strong> since multiple consequent element insertions will cause more layout changes.<\/p>\n<p>On the other hand, adding elements to a <code>DocumentFragment<\/code> object <strong>doesn\u2019t cause any layout changes<\/strong>, so you can add as many elements to it as you want before passing them to the DOM tree, causing a layout change only at this point.<\/p>\n<h3>Syntax<\/h3>\n<pre>document.createDocumentFragment()<\/pre>\n<h3>Code Example<\/h3>\n<p>In this example, we create multiple table rows and cells with the <code>createElement()<\/code> method, then add them to a <code>DocumentFragment<\/code> object, finally add that document fragment to a HTML <code>&lt;table&gt;<\/code> using the <code>appendChild()<\/code> method.<\/p>\n<p>As a result, five rows \u2013 each of them containing one cell with a number from 1 to 5 as content \u2013 will be inserted inside the table.<\/p>\n<pre>&lt;table&gt;&lt;\/table&gt;<\/pre>\n<pre>\r\nvar table = document.querySelector(\"table\");\r\nvar df = document.createDocumentFragment();\r\n\r\nfor(var i=0; i&lt;5; i++) {\r\n  var td = document.createElement(\"td\");\r\n  var tr = document.createElement(\"tr\");\r\n  td.textContent = i;\r\n  tr.appendChild(td)\r\n  df.appendChild(tr);\r\n}\r\n\r\ntable.appendChild(df);\r\n<\/pre>\n<h2>12. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/getComputedStyle\" target=\"_blank\" rel=\"noopener\"><code>getComputedStyle()<\/code><\/a><\/h2>\n<p>Sometimes you want to <strong>check the CSS property values<\/strong> of an element before making any changes. You can use the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/HTMLElement\/style\" target=\"_blank\" rel=\"noopener\"><code>ele.style<\/code><\/a> property to do the same, however the <code>getComputedStyle()<\/code> method has been made just for this purpose, it <strong>returns the read-only computed values<\/strong> of all the CSS properties of a specified HTML element.<\/p>\n<h3>Syntax<\/h3>\n<pre>var style = getComputedStyle(ele, [pseudoEle])<\/pre>\n<ul>\n<li><code>style<\/code> \u2013 A <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/CSSStyleDeclaration\" target=\"_blank\" rel=\"noopener\"><code>CSSStyleDeclaration<\/code><\/a> object returned by the method. It holds all CSS properties and their values of the <code>ele<\/code> element.<\/li>\n<li><code>ele<\/code> \u2013 The HTML element of which CSS property values are fetched.<\/li>\n<li><code>pseudoEle<\/code> \u2013 (optional) A string that represents a pseudo-element (for example, <code>':after'<\/code>). If this is mentioned, then the  CSS properties of the specified pseudo-element associated with <code>ele<\/code> will be returned.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we get and alert the computed <code>width<\/code> value of a <code>&lt;div&gt;<\/code> element by using the <code>getComputedStyle()<\/code> method.<\/p>\n<pre>&lt;div&gt;&lt;\/div&gt;<\/pre>\n<pre>\r\nvar style = getComputedStyle(document.querySelector('div'));\r\nalert(style.width);\r\n<\/pre>\n<h2>13. <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/Element\/setAttribute\" target=\"_blank\" rel=\"noopener\"><code>setAttribute()<\/code><\/a><\/h2>\n<p>The <code>setAttribute()<\/code> method either <strong>adds a new attribute<\/strong> to an HTML element, or <strong>updates the value<\/strong> of an attribute that already exists.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.setAttribute(name, value);<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 The HTML element to which an attribute is added, or of which attribute is\t updated.<\/li>\n<li><code>name<\/code> \u2013 The name of the attribute.<\/li>\n<li><code>value<\/code> \u2013 The value of the attribute.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we add the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Global_attributes\/contenteditable\" target=\"_blank\" rel=\"noopener\"><code>contenteditable<\/code><\/a> attribute to a <code>&lt;div&gt;<\/code> by making use of the <code>setAttribute()<\/code> method, which will turn its content editable.<\/p>\n<pre>&lt;div&gt;hello&lt;\/div&gt;<\/pre>\n<pre>\r\nvar div = document.querySelector('div');\r\ndiv.setAttribute('contenteditable', '')\r\n<\/pre>\n<h2>14. <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/Web\/API\/Element\/getAttribute\" target=\"_blank\" rel=\"noopener\"><code>getAttribute()<\/code><\/a><\/h2>\n<p>The <code>getAttribute()<\/code> method <strong>returns the value of a specified attribute<\/strong> belonging to a certain HTML element.<\/p>\n<h3>Syntax<\/h3>\n<pre>ele.getAttribute(name);<\/pre>\n<ul>\n<li><code>ele<\/code> \u2013 The HTML element of which attribute is requested.<\/li>\n<li><code>name<\/code> \u2013 The name of the attribute.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we alert the value of the <code>contenteditable<\/code> attribute belonging to the <code>&lt;div&gt;<\/code> element with the help of the <code>getAttribute()<\/code> method.<\/p>\n<pre>&lt;div contenteditable=true&gt;hello&lt;\/div&gt;<\/pre>\n<pre>\r\nvar div = document.querySelector('div');\r\nalert(div.getAttribute('contenteditable'))\r\n<\/pre>\n<h2>15. <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/removeAttribute\" target=\"_blank\" rel=\"noopener\"><code>removeAttribute()<\/code><\/a><\/h2>\n<p>The <code>removeAttribute()<\/code> method <strong>removes a given attribute<\/strong> of a specific HTML element.<\/p>\n<h3>Syntax<\/h3>\n<p><code>ele.removeAttribute(name);<\/code><\/p>\n<ul>\n<li><code>ele<\/code> \u2013 The HTML element of which attribute is to be removed.<\/li>\n<li><code>name<\/code> \u2013 The name of the attribute.<\/li>\n<\/ul>\n<h3>Code Example<\/h3>\n<p>In this example, we remove the <code>contenteditable<\/code> attribute from the <code>&lt;div&gt;<\/code> element. As a result, the <code>&lt;div&gt;<\/code> won\u2019t be editable any more.<\/p>\n<pre>&lt;div contenteditable=true&gt;hello&lt;\/div&gt;<\/pre>\n<pre>\r\nvar div = document.querySelector('div');\r\ndiv.removeAttribute('contenteditable');\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages, and based on this structure to render HTML elements on the screen. HTML defines the default DOM structure. However in many cases you may want to manipulate this&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[4117],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Master DOM Manipulation with 15 Essential JavaScript Methods - Hongkiat<\/title>\n<meta name=\"description\" content=\"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,\" \/>\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.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Master DOM Manipulation with 15 Essential JavaScript Methods\" \/>\n<meta property=\"og:description\" content=\"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-10T13:01:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-04-06T11:20:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"Master DOM Manipulation with 15 Essential JavaScript Methods\",\"datePublished\":\"2016-10-10T13:01:03+00:00\",\"dateModified\":\"2023-04-06T11:20:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/\"},\"wordCount\":1875,\"commentCount\":7,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/dom-manipulation-javascript-methods\\\/dom-tree.jpg\",\"keywords\":[\"Javascripts\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/\",\"name\":\"Master DOM Manipulation with 15 Essential JavaScript Methods - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/dom-manipulation-javascript-methods\\\/dom-tree.jpg\",\"datePublished\":\"2016-10-10T13:01:03+00:00\",\"dateModified\":\"2023-04-06T11:20:39+00:00\",\"description\":\"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/dom-manipulation-javascript-methods\\\/dom-tree.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/dom-manipulation-javascript-methods\\\/dom-tree.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/dom-manipulation-javascript-methods\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Master DOM Manipulation with 15 Essential JavaScript Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Master DOM Manipulation with 15 Essential JavaScript Methods - Hongkiat","description":"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,","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.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/","og_locale":"en_US","og_type":"article","og_title":"Master DOM Manipulation with 15 Essential JavaScript Methods","og_description":"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,","og_url":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2016-10-10T13:01:03+00:00","article_modified_time":"2023-04-06T11:20:39+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"Master DOM Manipulation with 15 Essential JavaScript Methods","datePublished":"2016-10-10T13:01:03+00:00","dateModified":"2023-04-06T11:20:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/"},"wordCount":1875,"commentCount":7,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg","keywords":["Javascripts"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/","url":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/","name":"Master DOM Manipulation with 15 Essential JavaScript Methods - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg","datePublished":"2016-10-10T13:01:03+00:00","dateModified":"2023-04-06T11:20:39+00:00","description":"As a web developer, you frequently need to manipulate the DOM, the object model that is used by browsers to specify the logical structure of web pages,","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/dom-manipulation-javascript-methods\/dom-tree.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/dom-manipulation-javascript-methods\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Master DOM Manipulation with 15 Essential JavaScript Methods"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-7kh","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=28165"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28165\/revisions"}],"predecessor-version":[{"id":66026,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/28165\/revisions\/66026"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=28165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=28165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=28165"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=28165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}