How to write into an HTML element in JavaScript?

Here, we are going to learn how to write text into an HTML element using JavaScript?
Submitted by IncludeHelp, on February 02, 2019

Writing into an HTML element

To write string/text into an HTML element, we use the following things:

  • There must be an HTML element like paragraph, span, div etc.
  • An HTML element must have an Id.
  • We access the HTML element by using the id (to access an HTML element using Id, we use an HTML DOM method getElementbyId()).
  • Then, we write the text to the HTML element by using an HTML DOM property innerHTML.


In this example, we have two paragraphs with p1 and p2 ids, we are writing two different texts in these HTML elements (paragraphs).


<title>JavaScipt Example</title>

	<p id="p1"></p>
	<p id="p2"></p>
		document.getElementById("p1").innerHTML = "Hello world!";
		document.getElementById("p2").innerHTML = 10+20;


Hello world!


JavaScript Tutorial »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.