Display Javascript Variable In Html Body
here is my approximate code: I need to display it norm
Solution 1:
So you want to append some text to <body>
?
function smth(str) {
return document.body.appendChild(document.createTextNode(str));
}
This is making use of the following DOM Methods
Please notice that it won't introduce formatting (such as line brakes <br />
), if you want those you'd need to add them too.
Solution 2:
With this approach you can target an element by ID and insert whatever you like inside it, but the solution suggested from Paul S is more simple and clean.
<html>
<head>
<script type="text/javascript">
var myVar = 42;
function displayMyVar(targetElementId) {
document.getElementById(targetElementId).innerHTML = myVar;
}
</script>
</head>
<body onload="displayMyVar('target');">
<span id="target"></span>
</body>
</html>
Solution 3:
One of the very common ways to do this is using innerHTML
. Suppose you declare a <p>
in the <body>
as output, then you can write:
<script> function smth(){........}
var name=smth('fullname');
var hobby=smth('hobby')
var out=document.getElementById("output");
out.innerHTML=name; (or you may write hobby)
</script>
Solution 4:
try using:
var yourvar='value';
document.body.innerHTML = yourvar;
Post a Comment for "Display Javascript Variable In Html Body"