0

Can we use variables assigned in the javascript anywhere in the HTML? What to do to assign that variable to other places of html

I have tried this but I failed to get an answer.

2nd Jul 2019, 11:06 AM
Abhra 2005
Abhra 2005 - avatar
1 Réponse
+ 1
JavaScript codes can only resided inside of <script></script>, we cannot simply put any variable assigned anywhere outside of script tag. You can only use Document methods such as getElementsByClassName, getElementById, to get Element object, then update the element accordingly using Element methods such as innerText, innerHTML. e.g. <div id="content"></div> <!- cannot put any js codes or variables here -> <script> var text = 'Hello';. // variable can only run from inside script tag document.getElementById('content').innerText = text; // innerText would update a text 'Hello' inside HTML element with id="content" </script>
2nd Jul 2019, 11:32 AM
Calviղ
Calviղ - avatar