+ 3

JavaScript inside html

How can I display a JavaScript variable inside an html element? I wanted a paragraph <p> to show var x = value, and I need it to be dynamic so I can change var x’s value but still be shown

9th Jun 2018, 7:56 PM
Tence Sailor
Tence Sailor - avatar
3 Respuestas
+ 5
<p id="test"></p> <script> window.onload=function(){ var x=10; var test=document.getElementById("test").innerHTML =x; } </script>
9th Jun 2018, 8:00 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 4
<p> var x = <span id=x></span> </p> <script> var x = document.getElementById('x'); // ...do what you want with x </script>
9th Jun 2018, 8:01 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
Shudarshan Rai that code worked like a charm. thanks
9th Jun 2018, 8:04 PM
Tence Sailor
Tence Sailor - avatar