0
HTML Display A JavaScript Variable
How do you do this?
3 odpowiedzi
+ 2
Probably better to use .innerHTML property of html element targeted to display the value of variable:
<div id="mydiv">text</div>
<input type="button" onclick="myfunc();" value="change content">
<script>
var myvalue = 42;
function myfunc() {
var d = document.getElementById("mydiv");
d.innerHTML = myvalue;
}
</script>
+ 1
There are different ways to display the content the variable actually has: You can give it out to the console with console.log(variable); write it into an element of the webpage with
document.getElementById("info").innerHTML = variable; or give it out into a textfield with document.getElementById("info").value = variable; to name just a few,
0
you can show it with document.write(variableName);