close

how to display a javascript var in html body

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn how to display a javascript var in html body in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

how to display a javascript var in html body ?

  1. how to display a javascript var in html body ?

    to display a javascript var in html body Here is another way it can be done .
    function showData(m) { let x ="<div> added from js "; let y = m.toString(); let z = "</div>"; let htmlData = x+y+z ; content.insertAdjacentHTML("beforeend",htmlData);

  2. how to display a javascript var in html body ?

    to display a javascript var in html body Here is another way it can be done .
    function showData(m) { let x ="<div> added from js "; let y = m.toString(); let z = "</div>"; let htmlData = x+y+z ; content.insertAdjacentHTML("beforeend",htmlData);

Method 1

<html>
<head>
<script type="text/javascript">
var number = 123;
var string = "abcd";

function docWrite(variable) {
    document.write(variable);
}
</script>
</head>

<body>
<h1>the value for number is: <script>docWrite(number)</script></h1>

<h2>the text is: <script>docWrite(string)</script> </h2>
</body>
</html>

You can shorten document.write but can’t avoid <script> tag

Method 2

Here is another way it can be done .

function showData(m)
{
    let x ="<div> added from js ";
    let y = m.toString();
    let z = "</div>";
    let htmlData = x+y+z ;
    content.insertAdjacentHTML("beforeend",htmlData);

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment