place html in javascript strings?
52,521
Solution 1
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>";
Solution 2
You can have it in one line:
document.getElementById("mydivtag").appendChild(function(li, l, t) { li.appendChild(function(a, l, t) { a.href = l; a.innerHTML = t; return a; } (document.createElement("a"), l, t)); return li; } (document.createElement("li"), "mylink", "mytext"));
Author by
user840930
Updated on July 05, 2022Comments
-
user840930 almost 2 years
I need to place html in a javascript string. 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?
-
Mustafa about 8 yearsit needs less jQuery