moving variables between js and html | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

moving variables between js and html

if I have a variable declared in the js section how can I use the this variabe in the html code

27th May 2021, 7:30 PM
T1xx1
T1xx1 - avatar
4 Answers
+ 2
could you provide your use case to better understand what you wantvto achieve? (it's not clear, because html doesn't handle variables ^^)
27th May 2021, 7:51 PM
visph
visph - avatar
+ 2
you must get a reference of the targeted element in wich you want display the value (as text) an assign it to its 'textContent' property (or innerHTML if you need to provide html code to be parsed) for most of elements: for input/form elements the property to assign is usually 'value'... the easiest way to get a reference is to use querySelector or querySelectorAll methods (wich get a query string argument -- css selectors syntax), either on document or on another element (wich will be used as root for searching in childs at any level). https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
27th May 2021, 8:14 PM
visph
visph - avatar
0
I know that html doesn't handle variables so i'm asking how can I use some variables declared in the js script for showing them in the web page using the html. like this: I have a web project in the code editor of sololearn and in the js section I have declared a variable named "post". How I can show the value of variable in the web page (html manage the web page that's why i'm asking how can I show variables in html but declared in js)
27th May 2021, 8:00 PM
T1xx1
T1xx1 - avatar
0
Ok, thanks
27th May 2021, 8:19 PM
T1xx1
T1xx1 - avatar