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>
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() .
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.
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.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With