place html in javascript?

user840930 picture user840930 · Apr 30, 2012 · Viewed 50.6k times · Source

I need to place html in javascript. Specifically, I would like to add an HTML link to a div tag with javascript.

html:

<div id="mydivtag"></div>

javascript:

document.getElementById('mydivtag').innerHTML = "<li><a href=\"someLink\">Some Link</a></li> ";

Am I formatting the html link I am adding through javascript correctly?

Answer

Andy Gaskell picture Andy Gaskell · Apr 30, 2012

Looks fine and works here. You may want to consider mixing single quotes instead of escaping the double quotes, but that's just a preference.

document.getElementById('mydivtag').innerHTML = "<li><a href='someLink'>Some Link</a></li>";