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
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).
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.
var s = '<div id="myDiv"></div>'; var htmlObject = document.createElement('div'); htmlObject.innerHTML = s; htmlObject.getElementById("myDiv").style.marginTop = something;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With