{"id":10573,"date":"2019-10-14T15:09:27","date_gmt":"2019-10-14T19:09:27","guid":{"rendered":"https:\/\/www.carnaghan.com\/?p=10573"},"modified":"2021-03-06T15:48:23","modified_gmt":"2021-03-06T20:48:23","slug":"javascript-manipulating-the-document-object-model-dom","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/javascript-manipulating-the-document-object-model-dom\/","title":{"rendered":"JavaScript Manipulating the Document Object Model (DOM)"},"content":{"rendered":"\n
The way in which logical structure of a document is accessed and manipulated in HTML is known as Document Object Model or DOM. It is a programming interface for HTML. Whenever a document loads in the browser, a tree-like (or forest) structure is created. Methods provided by the DOM can be used to access and manipulate this tree programmatically.<\/p>\n\n\n\n
The DOM is used to access and manipulate elements in an HTML document. But why do we need to use the DOM for this? Let’s understand this by use of an example. Let’s suppose we have an input field and a button next to it. When the button is clicked, whatever typed in the input field should appear below the input field. How can we do this with plain HTML? We need to dynamically access the value of the input field on the button click and then display the value below in a paragraph. We will implement this example, but first, we should discuss DOM methods that will be needed for accessing and manipulating elements.<\/p>\n\n\n\n
There are three basic methods of DOM for\naccessing elements.<\/p>\n\n\n\n
Out of these three, getElementById() is the most commonly used method. There are a few properties that can be used with these methods:<\/p>\n\n\n\n
The DOM also provides methods for adding and deleting elements.<\/p>\n\n\n\n
Earlier, we discussed an example of displaying data typed in the input field, let’s take a look at this in more detail. This is the HTML code for the input field and the button.<\/p>\n\n\n\n
<input type=\"text\" id=\"input\" \/>\n<button onclick=\"myFunction()\">\n Display\n<\/button>\n<p id = \"para\" > <\/p><\/code><\/pre>\n\n\n\nWhen the button is clicked, myFunction()\nwill be invoked. We will access the input field using the\ndocument.getElementById() method.<\/p>\n\n\n\n
var value = document.getElementById(\"input\").value;<\/code><\/pre>\n\n\n\nHere we are using the value property to access the value of the input text field, and then we are storing it in the value variable<\/strong>. Now we have the value, the next step is to display this value in the paragraph. But first, we need to access the paragraph. Again, we will use the document.getElementById() method.<\/p>\n\n\n\ndocument.getElementById(\"para\")<\/code><\/pre>\n\n\n\nBut we also need to change its value.\nCurrently, its value is nothing. So to change its value, we will use the\ninnerHTML property.<\/p>\n\n\n\n
document.getElementById(\"para\").innerHTML = value;<\/code><\/pre>\n\n\n\nNow, let’s put this altogether in one file.<\/p>\n\n\n\n
<html>\n <body>\n <input type=\"text\" id=\"input\" \/>\n\t<button onclick=\"myFunction()\">Display<\/button>\n\t<p id = \"para\" > <\/p>\n <\/body>\n <script>\n function myFunction(){\n var value = document.getElementById(\"input\").value;\n document.getElementById(\"para\").innerHTML = value;\n }\n <\/script>\n<\/html><\/code><\/pre>\n\n\n\nThis is how we access and manipulate\nelements using DOM.<\/p>\n\n\n\n
Changing style <\/h2>\n\n\n\n
We can also change the style of an element\nusing DOM. Consider the following HTML code.<\/p>\n\n\n\n
<p onclick=\"myFunction()\" id=\"para\"> Click on this paragraph to change its color. <\/p><\/code><\/pre>\n\n\n\nThere is a paragraph. Clicking on this\nparagraph will invoke myFunction(). We will change the color as well as the\ntext of this paragraph.<\/p>\n\n\n\n
To change the color, we will use the\nstyle.property method.<\/p>\n\n\n\n
document.getElementById(\"para\").style.color = \"red\";<\/code><\/pre>\n\n\n\nTo change the text, we will use the\ninnerHTML property.<\/p>\n\n\n\n
document.getElementById(“para”).innerHTML\n= “Color of this paragraph is red”;<\/p>\n\n\n\n
document.getElementById(\"para\").innerHTML = \"Color of this paragraph is red\";<\/code><\/pre>\n\n\n\nHere, we again used the\ndocument.getElementById() method to access the element.<\/p>\n\n\n\n
Let’s put all of this together.<\/p>\n\n\n\n
<html>\n <body>\n <p onclick=\"myFunction()\" id=\"para\"> Click on this paragraph to change its color. <\/p>\n <\/body>\n <script>\n function myFunction(){\n\t document.getElementById(\"para\").style.color = \"red\";\n\t document.getElementById(\"para\").innerHTML = \"Color of this paragraph is red\";\n }\n <\/script>\n<\/html><\/code><\/pre>\n\n\n\nNow, when the text is clicked, its color,\nas well as the text of the paragraph, will be changed.<\/p>\n\n\n\n
Creating an element <\/h2>\n\n\n\n
The DOM also provides methods to create a new element. Let’s create a paragraph and adding into the body using these methods.<\/p>\n\n\n\n
The first step is to create a paragraph\nusing the createElement() method.<\/p>\n\n\n\n
document.createElement(\"P\");<\/code><\/pre>\n\n\n\nWe need to store it into a variable.<\/p>\n\n\n\n
var newPara = document.createElement(\"P\");<\/code><\/pre>\n\n\n\nWe have a paragraph, now we need to add\nsome text into it. We will use the innerHTML property to add the text.<\/p>\n\n\n\n
newPara.innerHTML = \"This is a paragraph\"<\/code><\/pre>\n\n\n\nOur element is ready. Let’s append it to\nthe body of the document using the appendChild() method.<\/p>\n\n\n\n
document.body.appendChild(newPara)<\/code><\/pre>\n\n\n\nEverything is ready now. But we need\nsomething to implement all this. Let’s create a button for this.<\/p>\n\n\n\n
<button onclick=\"myFunction()\"> Click <\/button><\/code><\/pre>\n\n\n\nLet’s put all this in one file.<\/p>\n\n\n\n
<html>\n <body>\n\t<button onclick=\"myFunction()\"> Click to add an element <\/button>\n <\/body>\n <script>\n function myFunction(){\n\t var newPara = document.createElement(\"P\")\n\t newPara.innerHTML = \"This is a paragraph\"\n\t document.body.appendChild(newPara)\n }\n <\/script>\n<\/html><\/code><\/pre>\n\n\n\nWhen the button is clicked, a new paragraph\nwill be created and appended to the body of the document.<\/p>\n\n\n\n
querySelector()<\/h2>\n\n\n\nquerySelector()<\/code><\/pre>\n\n\n\nUntil now, we discussed three ways of selecting elements. We used getElementById(), however there is another method we can use called the querySelector() method.<\/p>\n\n\n\n
The querySelector() method returns only the\nvery first matched element. Let’s understand with the help of an example.<\/p>\n\n\n\n
<html>\n <body>\n\t<button onclick=\"myFunction()\">Click <\/button>\n\t<p class=\"para\"> This is the first paragraph <\/p>\n\t<p class=\"para\"> This is the second paragraph <\/p>\n\t<p class=\"para\"> This is the third paragraph <\/p>\t\n <\/body>\n <script>\n function myFunction(){\n\t document.querySelector(\".para\").style.color = \"red\"\n }\n <\/script>\n<\/html><\/code><\/pre>\n\n\n\nThe above example has a button and three\nparagraphs. All the paragraphs have a common class. In the script, inside\nmyFunction(), we have used the querySelector to change the color where the\nclass is “para”. Since each paragraph has the same class, what do you\nthink will happen here? Colors of all the paragraphs will change? No! Because\nwe are using querySelector, the color of only the first matching paragraph will\nchange. <\/p>\n\n\n\n
This is how the querySelector() method\nworks. But if we want to match all the elements, we can use the\nquerySelectorAll() method.<\/p>\n\n\n\n
Wrapping it up<\/h2>\n\n\n\n
So this is the basic overview of DOM and\nhow to use its methods and properties to access and manipulate elements. These\nwere the basic as well as the most commonly used methods in DOM manipulation.\nWhen you will dive deeper, you will find many other methods and properties. DOM\nmanipulation is one the easiest and at the same time, one the most important\npart of web application development.<\/p>\n","protected":false},"excerpt":{"rendered":"
The way in which logical structure of a document is accessed and manipulated in HTML is known as Document Object Model or DOM. It is a programming interface for HTML. Whenever a document loads in the browser, a tree-like (or forest) structure is created. Methods provided by the DOM can be used to access and […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[11499,8212,7349],"post_series":[7348],"yoast_head":"\n
JavaScript Manipulating the Document Object Model (DOM) - Ian Carnaghan<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n