×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

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.

Example

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

<html>
<head>
<title>JavaScipt Example</title>
</head>

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

Output

Hello world!

30

JavaScript Tutorial »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.