Javascript decides behaviour of web page. Using javascript you can edit, delete, change and display the HTML element and it’s content.
This post is about how HTML content is modified by javascript.
HTML DOM elements allow javascript to change content of HTML element.
What is JavaScript HTML DOM?
The HTML DOM (Document Object Model) is logical structure of HTML elements. It represents each node (element) as object. It defines methods, properties and events for all HTML elements. DOM is constructed as logical tree of objects.
For Javascript HTML DOM is Application Programming Interface (API). Javascript can add, delete, and modify elements, it’s attribute, it’s CSS styles and events.
Before changing element’s content first you have to find HTML element.
Finding and accessing HTML element by DOM methods and property
• By id
<!DOCTYPE html> <html> <head> <title>Finding and accessing elements</title> </head> <body> <p id="fetch">Fetching content by id</p> <p id="demo"></p> <script> var e= document.getElementById("fetch"); document.getElementById("demo").innerHTML="The content of paragraph is:- "+e.innerHTML; </script> </body> </html>
Output:-
• By class name
<!DOCTYPE html> <html> <head> <title>Finding and accessing elements</title> </head> <body> <h2 class="find">Fetching content by class name</h2> <h3 id="display"></h3> <script> var a=document.getElementsByClassName("find"); document.getElementById("display").innerHTML="The heading is:-"+ a[0].innerHTML; </script> </body> </html>
Output:-
• By tag name
<!DOCTYPE html> <html> <head> <title>Finding and accessing elements</title> </head> <body> <p>Fetching content by tag name.</p> <p id="tags"></p> <script> var t=document.getElementsByTagName("p"); document.getElementById("tags").innerHTML="The content is:-"+t[0].innerHTML; </script> </body> </html>
Output:-
• By CSS selector
<!DOCTYPE html> <html> <head> <title>Finding and accessing elements</title> </head> <body> <p class="demo">Hello World</p> <p id="sel"></p> <script> var q=document.querySelectorAll("p.demo"); document.getElementById("sel").innerHTML="Content is:-"+q[0].innerHTML; </script> </body> </html>
Output:-
• By HTML object
<!DOCTYPE html> <html> <head> <title>Finding and accessing elements</title> </head> <body> <p id="sel"></p> <script> document.getElementById("sel").innerHTML="The title is:"+document.title; </script> </body> </html>
Output:-
How to change the content of HTML element?
<!DOCTYPE html> <html> <head> <title>Change content</title> </head> <body> <h1 id="i1">Welcome</h1> <p>The text welcome is changes to text Hello.</p> <script> var head=document.getElementById("i1"); document.getElementById("i1").innerHTML="Hello"; </script> </body> </html>
Output:-
Modify the CSS style by dom method
<!DOCTYPE html> <html> <head> <title>Change content</title> </head> <body> <p id="js">Hello Javascript!</p> <script> var s=document.getElementById("js"); document.getElementById("js").style.fontSize="24px"; document.getElementById("js").style.fontWeight="bold"; document.getElementById("js").style.color="red"; </script> </body> </html>
Output:-
Modifying HTML attribute value by DOM method
<!DOCTYPE html> <html> <head> <title>Change Content</title> </head> <body> <input type="text" name="text" id="txt" value="Hello"> <button type="button" onclick='document.getElementById("txt").value="HTML"'>clickme</button> </body> </html>
Output:-
<!DOCTYPE html> <html> <head> <title>Change Content</title> </head> <body> <img id="pic" src="/Drawing/image_2021-09-01_22-02.jpg" height="200" width="200"></img> <script> document.getElementById("pic").src="/Drawing/image_2021-09-04_21-19.png"; </script> </body> </html>
Output:-
Hiding/showing HTML element by DOM method
<!DOCTYPE html> <html> <head> <title>Change Content</title> </head> <body> <img id="pic" src="/storage/emulated/0/Drawing/image_2021-09-01_22-02.jpg" height="200" width="200"></img> <script> document.getElementById("pic").style.display="none"; </script> </body> </html>
how to change and delete the list items using HTML DOM method.
<!DOCTYPE html> <html> <head> <title>Edit list</title> <style> div{ height:auto; width:300px; padding:10px; background-color:#033; box-shadow:5px 3px 3px #000;} h2{ color:#f00; text-align:center;} li{ color:#ffc;} button{ margin-left:20px; color:#aaa; background-color:#b02; border-radius:10px;} </style> </head> <body> <div> <h2>Fruit list for buying</h2> <ol id="fruit"> <li id="f1">Apple</li> <li id="f2">Cherry</li> <li id="f3">Banana</li> <li id="f4">Grapes</li> </ol> <button type="button" onclick="change()" >Change list</button> <button type="button" onclick="del()" >Hide list</button> </div> <script> function change() { document.getElementById("f1").innerHTML="Mango"; document.getElementById("f2").innerHTML="Orange"; document.getElementById("f3").innerHTML="Gauva"; document.getElementById("f4").innerHTML="Lichi"; } function del() { document.getElementById("fruit").style.display="none"; } </script> </body> </html>
Output:-
Following example change emoji , replace text with emoji and also hide the text using HTML DOM method.
<!Doctype html> <html lang="en"> <head> <title></title> <meta charset="UTF-8"> <style> button{height:50px; width:100px; color:#fff; background-color:black;} </style> </head> <body> <p id="smile" style="font-size:55px">😃</p> <button type="button" onclick='document.getElementById("smile").innerHTML="😞"'>SAD</button> <p id="smile1" style="font-size:55px">I'm Happy.</p> <button type="button" onclick='document.getElementById("smile1").innerHTML="😊"'>Click me!</button> <p id="hide" style="font-size:55px">Hide me!</p> <button type="button" onclick='document.getElementById("hide").style.display="none"'>Hide</button> </body> </html>
Output:-
The getElementById() is easy and most popular method. It directly work with HTML.
There is a minor difference in between document.getElementById() and document.getElementsByClassName() and document.getElementsByTagName() methods.
The difference is of word element. The dom id method contains "element" and dom classname and tagname methods contains "elements".
0 Comments