Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a javascript string to a html object [duplicate]

can I convert a string to a html object? like:

string s = '<div id="myDiv"></div>'; var htmlObject = s.toHtmlObject; 

so that i can later on get it by id and do some changing in its style

var ho = document.getElementById("myDiv").style.marginTop = something; 

Thanx a million in advance, Lina

like image 933
bombo Avatar asked Mar 26 '10 10:03

bombo


People also ask

What is parseHTML in Javascript?

parseHTML uses native methods to convert the string to a set of DOM nodes, which can then be inserted into the document. These methods do render all trailing or leading text (even if that's just whitespace).

How do you change a string in HTML?

The simplest way to do this is to create an element, insert the string into with innerHTML , then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template HTML */ var stringToHTML = function (str) { var dom = document.


1 Answers

var s = '<div id="myDiv"></div>'; var htmlObject = document.createElement('div'); htmlObject.innerHTML = s; htmlObject.getElementById("myDiv").style.marginTop = something; 
like image 159
pawel Avatar answered Nov 15 '22 16:11

pawel