Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  129] [ 5]  / answers: 1 / hits: 22788  / 14 Years ago, sat, december 4, 2010, 12:00:00

I want to create HTMLElement from string by javasacript, like this



element = createHTMLElement('<table class=list><tr><td><a href=xxx>title</a></td></tr></table>')
element.addEventListener(...)
parent.appendChild(element)


and I do not want to use jQuery


More From » javascript

 Answers
110

You can create some dummy outer element:



  var div = document.createElement('DIV');


and then:



  div.innerHTML = '<table class=list><tr><td><a href=xxx>title</a></td></tr></table>'


and then extract it from childNodes:



  div.firstChild


innerHTML is a Microsoft extension, but one universally supported on all modern browsers.



Of course you can form a simple function which does what you want from these snippets.


[#94729] Thursday, December 2, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karivictoriab

Total Points: 530
Total Questions: 90
Total Answers: 95

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;