
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is it possible to change the 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
Output
- Related Articles
- Is it possible to change the value of the function parameter in JavaScript?
- Is it possible to change values of the array when doing foreach() in javascript?
- Is it possible to change directory by using File object in Java?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to select text boxes with JavaScript?
- Is it possible to use JSF+Facelets with HTML 4/5?
- Is it possible to display substring from object entries in JavaScript?
- Is it possible to create a new data type in JavaScript?
- Is it possible to have an HTML canvas element in the background of my page?
- Is it possible to de-structure to already-declared variables? In JavaScript?
- Is it possible to write data to file using only JavaScript?
- Is it possible to have JavaScript split() start at index 1?
- How to adjust the width and height of an iframe to fit with content in it HTML?
- Is it possible to have a HTML SELECT/OPTION value as NULL using PHP?
- HTML content Attribute
