66

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>
atom715
  • 781
  • 1
  • 5
  • 5

9 Answers9

73

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>
Ashwin Pandey
  • 125
  • 13
Priyank_Vadi
  • 1,028
  • 10
  • 27
50

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>
kj_
  • 703
  • 4
  • 9
4
<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

recnac
  • 3,744
  • 6
  • 24
  • 46
ajaykools
  • 645
  • 7
  • 12
0

<script type="text/javascript">
        function get_param(param) {
   var search = window.location.search.substring(1);
   var compareKeyValuePair = function(pair) {
      var key_value = pair.split('=');
      var decodedKey = decodeURIComponent(key_value[0]);
      var decodedValue = decodeURIComponent(key_value[1]);
      if(decodedKey == param) return decodedValue;
      return null;
   };

   var comparisonResult = null;

   if(search.indexOf('&') > -1) {
      var params = search.split('&');
      for(var i = 0; i < params.length; i++) {
         comparisonResult = compareKeyValuePair(params[i]); 
         if(comparisonResult !== null) {
            break;
         }
      }
   } else {
      comparisonResult = compareKeyValuePair(search);
   }

   return comparisonResult;
}

var parcelNumber = get_param('parcelNumber'); //abc
var registryId  = get_param('registryId'); //abc
var registrySectionId = get_param('registrySectionId'); //abc
var apartmentNumber = get_param('apartmentNumber'); //abc

        
    </script>

then in the page i call the values like so:

 <td class="tinfodd"> <script  type="text/javascript">
                                                    document.write(registrySectionId)
                                                    </script></td>
0

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);
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

You can do the same on document ready event like below

<script>
$(document).ready(function(){
 var number = 112;
    $("yourClass/Element/id...").html(number);
// $("yourClass/Element/id...").text(number);
});
</script>

or you can simply do it using document.write(number);.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Lego
  • 15
  • 2
-1
<?php
$x1='<span id="x1"></span><script>document.getElementById("x1").innerHTML = x1;</script>';
$x2='<span id="x2"></span><script>document.getElementById("x2").innerHTML = x2;</script>';
$x3='<span id="x3"</span><script>document.getElementById("x3").innerHTML = x3;</script>';
?>

<html><body>
<script> var 
  x1="123",
  x2="ABC", 
  x3=666; 
</script>
<?php echo $x1 ?><br>
<?php echo $x2 ?><be>
<?php echo $x3 ?><be>
</body></html>
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 07 '21 at 05:12
-2

Index.html:

<html>
<body>
    Javascript Version: <b id="version"></b>
    <script src="app.js"></script>
</body>
</html>

app.js:

var ver="1.1";
document.getElementById("version").innerHTML = ver;
lawrghita
  • 29
  • 6
-4

You cannot add JavaScript variable to HTML code.

For this you need to do in following way.

<html>
<head>
<script type="text/javscript">
var number = 123;

document.addEventListener('DOMContentLoaded', function() {
   document.getElementByTagName("h1").innerHTML("the value for number is: " + number);
});
</script>
</head>
<body>
<h1></h1>
</body>
</html>
spankajd
  • 934
  • 7
  • 13