
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change HTML Content in JavaScript
Yes, it is possible to change the content of the HTML in javascript. Usually HTML content will be in HTML tags such as <p>, <span>, etc. In javascript, we have DOM methods that have an ability to access the HTML tags. Those methods, such as document.getElementById(), document.getElementByTagName(), etc., make use of tags to change the HTML content.
Example-1
In the following example, the content in the span tag is changed using a javascript DOM method document.getElementById().
<html> <body> <span id="change">Is javaScript is java.</span> <input type = "button" value = "change" onclick='document.getElementById("change").innerHTML = "No JavaScript is not java!"'> </body> </html>
Once the above code is executed, we will get the following on the screen
If we click on the above 'change' button we will get the following as output
Output
Example-2
In the following example, the content in the paragraph tag is changed using a Javascript DOM method.
<html> <body> <p id="change">Elon musk has failed 3 times</p> <input type = "button" value = "change" onclick='document.getElementById("change").innerHTML = "Elon musk has succeded in his fourth attempt"'> </body> </html>
Once the above code is executed, we will get the following on the screen
If we click on the above 'change' button we will get the following as output