How do I use HTML and JS together? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I use HTML and JS together?

* Note that I don't mean connecting JS to HTML using <script>! I've already finished HTML and CSS courses and now I'm doing JS course. Everything's fine, but there's one thing I don't understand. How do I use the information created/changed with JS inside HTML? Let's say I used JS to create a basic object person which has a basic property name with value "John". How do I now provide person.name to the exact paragraph of HTML? The only thing I know now that kinda do the thing is document.write(), but it's not really versatile. P.S. Tried to google, but it didn't actually help.

20th Jan 2020, 10:48 AM
Rick Sanchez
Rick Sanchez - avatar
4 Answers
+ 1
you can't do it in the HTML code! You need to use the document.getElementById(var) function of JS to write into the tag! here's a link to a stack overflow post explaining that: https://stackoverflow.com/questions/30035932/how-do-i-use-this-javascript-variable-in-html/30035967 hope it helps!
20th Jan 2020, 11:01 AM
Cairon Rodrigues
+ 3
Never use document.write. document.write writes your whole file with javascript. Create an span tag give it some id . Do document.getElementById("your span id") ; And use innerHTML or innerText
20th Jan 2020, 1:33 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar
+ 2
@Clara Rodrigues Yes it does help! That's pretty much what I needed. Thanks a lot.
20th Jan 2020, 12:09 PM
Rick Sanchez
Rick Sanchez - avatar
20th Jan 2020, 1:36 PM
🇮🇳Vivek🇮🇳
🇮🇳Vivek🇮🇳 - avatar