Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display a javascript var in html body

I am looking for a way to call a javascript number in the body of an html page. This does not have to be long and extravagant just simply work, I just want something like:

<html> <head> <script type="text/javscript"> var number = 123; </script> </head>  <body> <h1>"the value for number is: " + number</h1> </body> </html> 
like image 321
atom715 Avatar asked Nov 29 '16 05:11

atom715


People also ask

How do I display JavaScript results in HTML?

JavaScript Display PossibilitiesWriting into the HTML output using document.write() . Writing into an alert box, using window.alert() . Writing into the browser console, using console.log() .

How do you put a JavaScript variable in HTML?

You cannot use js variables inside html. To add the content of the javascript variable to the html use innerHTML() or create any html tag, add the content of that variable to that created tag and append that tag to the body or any other existing tags in the html.

Can you use VAR in HTML?

The <var> element in HTML defines a text as a variable and can be used to define variables of a mathematical expression or variables of a piece of code. The text enclosed in the <var> element is normally displayed in italics.


2 Answers

Try This...

<html>    <head>    <script>      function myFunction() {        var number = "123";        document.getElementById("myText").innerHTML = number;      }    </script>  </head>    <body onload="myFunction()">      <h1>"The value for number is: " <span id="myText"></span></h1>    </body>    </html>
like image 124
Priyank_Vadi Avatar answered Sep 27 '22 22:09

Priyank_Vadi


Use document.write().

<html>  <head>    <script type="text/javascript">      var number = 123;    </script>  </head>    <body>      <h1>        the value for number is:        <script type="text/javascript">          document.write(number)        </script>      </h1>  </body>  </html>
like image 41
kj_ Avatar answered Sep 27 '22 22:09

kj_