I am trying to display some JavaScript variable on my HTML page.
I was first using document.write() but it use to overwrite the current page when the function was called.
After searching around, the general consensus was that document.write() isn’t liked very much. What are the other options?
I found a page suggesting using .innerHTML but that was written in 2005.
A jsFiddle illustrating my problem http://jsfiddle.net/xHk5g/
Element.innerHTMLis pretty much the way to go. Here are a few ways to use it:HTML
JavaScript
If you just want to update a portion of a
<div>I usually just add an empty element with a class likevalueor one I want to replace the contents of to the main<div>. e.g.Then I’d use some code like this:
Then the HTML/DOM would now contain:
Full example. Click run snippet to try it out.