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

Same variable for html and js?!

How can I write a variable in Js which is also in HTML ?! e.g. <html> <head> </head> <body> <form> <input type="text" id="age" placeholder="write your age here" name="age"> </form> <script> document.write("age") // or : alert("age") </script> </body> </html> what's wrong in this code?! how can i use the same attribute in both HTML and JS?!

1st Oct 2016, 9:08 AM
Ahmad Badghaish
Ahmad Badghaish - avatar
5 Answers
+ 2
maybe you forgot to put ; in it
4th Oct 2016, 12:05 PM
Sjoerd Flameling
Sjoerd Flameling - avatar
0
use name. you used id. replace this
1st Oct 2016, 11:06 AM
HawkEye
HawkEye - avatar
0
@jack sully. I used them both but none of them worked
1st Oct 2016, 1:16 PM
Ahmad Badghaish
Ahmad Badghaish - avatar
0
i actually did not really catch what is you mean. but if you try to show the person age which is in the input, you need to get the element value first. here's the code : var ages = document.getElementById("age"); document.write(ages.value);
1st Oct 2016, 9:54 PM
Krisna A. Haryadi
Krisna A. Haryadi - avatar
0
You shouldn't use the same value for different attributes in the same tag
18th May 2018, 1:38 PM
Sjoerd Flameling
Sjoerd Flameling - avatar