I’m not sure how to phrase my question, so I’ll just post my code and explain what I’m trying to do:
function getNewElement(tagName, className, idName, text){
var newElement = document.createElement(tagName);
newElement.className = className;
newElement.id = idName;
newElement.innerHTML = text;
return newElement;
}
If I call
getNewElement("div", "meow", "meow1", getNewElement("span","meow", "meow2", "blahblahblah"));
I just get
<div id="meow1" class="meow">[object HTMLSpanElement]</div>
So my question is, how would I write this to return a string with out converting (potentially expensive operation?) or ghetto patching with strings.
Update:
ghetto patch version:
function getNewElement(tagName, className, idName, text){
return '<' + tagName + ' class=' + className + ' id=' + idName + '>' + text + '</' + tagName + '>';
}
Achieves the functionality I wanted, but I feel like it’s not that elegant.
Not sure, but if you want the contents of
#meow2in the new element#meow1, using the statement as you do, this would be a solution:now
would create a new element
#meow1, with the content of a newly created element#meow2. Appended somewhere in the document it would look like:Otherwise, if you want
#meow2to be a child of#meow1, this would be a solution:Now if you would do:
this would be the result: