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>
?
functionsmth(str) {
returndocument.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><scripttype="text/javascript">var myVar = 42;
functiondisplayMyVar(targetElementId) {
document.getElementById(targetElementId).innerHTML = myVar;
}
</script></head><bodyonload="displayMyVar('target');"><spanid="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>functionsmth(){........}
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"