Home » JavaScript

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).

Example:

<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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.