Javascript innerHTML
Posted by Superadmin on May 03 2023 02:31:23

Javascript innerHTML

By Priya PedamkarPriya Pedamkar
  

Javascript innerHTML

Introduction to Javascript innerHTML

InnerHTML property is used for modifying the HTML content from JavaScript code. InnerHTML property is also used for writing dynamic html content on html documents. Most of the cases InnerHTML property is used for writing dynamic html content like registration form, feedback form, comment form, etc. In this topic, we are going to learn about Javascript Innerhtml.

Real-Time Example: Let’s take Facebook.com as an example. While writing feedback, you click the Feedback link. That link takes you to a Feedback form. Here we are using innerHTML property to achieve the requirement.

How Does innerHTML Work in JavaScript?

Syntax:

document.getElementById(id).innerHTML = Modifiable HTML content

Explanation:

Example:

<p id="name">Paramesh</p>

Examples with Code

Here are the following examples as mentioned below:

Example #1 – Content Changing

Changing paragraph content based on Id property with innerHTML property:

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1>Changing paragraph content based on Id property</h>
</font>
<p id="id1">Developed an Enterprise Content Management online application by using Spring MVC as Back end and JSP, HTML, CSS, JavaScript and Angular JS as Front end.</p>
<p id="id2">Create a user with user type and admin type, where admin can allow to perform administration actions like modify user type and delete the users.</p>
<script>
document.getElementById("id1").innerHTML = "First Paragraph";
document.getElementById("id2").innerHTML = "Second Paragraph";
</script>
</body>
</html>

Output:

Javascript Innerhtml output 1

Explanation:

Example #2 – Changing Header content based on Id property

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1>Changing Header content based on Id property</h>
</font>
<h3 id="id1">I am first header h3 content</p>
<h3 id="id2">I am seocnd header h3 content</p>
<h3 id="id3">I am third header h3 content</p>
<script>
document.getElementById("id1").innerHTML = "First Header";
document.getElementById("id2").innerHTML = "Second Header";
document.getElementById("id3").innerHTML = "Third Header";
</script>
</body>
</html>

Output:

Javascript Innerhtml output 2

Explanation:

Example #3 – Changing the content of the anchor tag based on id

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1>Alerting The User about Changing conntent</h><br>
</font>
<script>
function getMyOutput()
{
document.getElementById("id").innerHTML = "This is the modified content portion";
alert(document.getElementById("id").innerHTML)
}
</script>
<font color="blue">
<a id="id" onclick='getMyOutput()'>Click Here</a>
</font>
</body>
</html>

Before Clicking OK button-Output:

Javascript Innerhtml output 3

After Clicking OK button-Output:

Javascript Innerhtml output 4

Explanation:

Example #4 – Changing the content of the button based on id

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1>Changing the content of button based on id</h><br>
</font>
<script>
function getMyOutput()
{
document.getElementById("id").innerHTML = "Changed Button";
}
</script>
<font color="blue">
<button id="id" onclick='getMyOutput()'>Initial Button</button>
</font>
</body>
</html>

 Before clicking Initial Button Output:

output 5

 After clicking Initial Button Output:

output 6

 Explanation:

Writing dynamic html content with innerHTML property

Example #1 – Generating Feedback form based on id

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1>Writing dynamic html content with innerHTML property</h><br>
</font>
<script>
function getFeedbackForm() {
var content="Your Name:<input type='text' name='name'><br>Email ID:<input type='text' name='email'><br>Mobile Number:<input type='text' name='mobile'><br>Feedback:<br><textarea rows='6' cols='90'></textarea><br><input type='submit' value='Send Feedback'>";
document.getElementById('feedbackID').innerHTML=content;
}
</script>
<form name="feedBack">
<input type="button" value="Feedback" onclick="getFeedbackForm()">
<div id="feedbackID"></div>
</form>
</body>
</html>

Output:

 output 7

Explanation:

Example #2 – Changing the Text Field Content dynamically based on id

Code:

<!DOCTYPE html>
<html>
<body>
<font color="green">
<h1> Changing the Content from TextField </h1>
</font>
<script>
function getMyText(){
var content = document.getElementById('typeContent').value;
document.getElementById('name').innerHTML = content;
}
</script>
<input type='text' id='typeContent' value='Enter Yor Name' />
<input type='button' onclick='getMyText()' value='Show My Name'/>
<p>Hi, I am  <b id='name'>Paramesh</b> </p>
</body>
</html>

Before clicking button-Output: m

output 8

After Clicking Button-Output:

output 9

Explanation: