I'm trying to replace a bunch of innerHTML references in code with DOM-compliant node stuff.
<html>
<body>hehe</body>
<script type="text/javascript">
var myDiv = document.createElement('div');
myDiv.setAttribute('id', 'myDivID');
document.getElementsByTagName('body')[0].appendChild(myDiv);
var textNode = '<p>This will be dynamically generated.</p>';
myDiv.appendChild(textNode);
</script>
</html>
Sure, the script isn't supposed to be below the body, but if I place it above, the javascript returns the error:
document.getElementsByTagName("body")[0]
is undefined
since the body doesn't exist yet.
But as it is now, the javascript returns the even more esoteric error on the "appendChild" step:
uncaught exception:
[Exception... "Could not convert JavaScript argument arg 0 [nsIDOMHTMLDivElement.appendChild]"
nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"
location: "JS frame :: .....TestStuff/wut.jsp :: <TOP_LEVEL> :: line 8"
data: no]
So what's the deal, here? You can repalce the myDiv.setAttribute
and myDiv.appendChild
lines with getElementById
references and the result is the same.
Edit: sorry, the above errors are firefox ones, not chrome ones. The chrome error is: Uncaught Error: NOT_FOUND_ERR: DOM Exception 8 on the myDiv.appendChild line. I don't get why myDiv wouldn't be on the DOM at this point, but chrome doesn't seem to think it is.
Edit 2: So, this line:
var textNode = '<p>This will be dynamically generated.</p>';
if made into
var textNode = document.createTextNode('< p>This will be dynamically generated.< /p>');
Will output text correctly. However, I want to know how to generate and append a chunk of html that will output as actual markup rather than pure text--something that can emulate innerHTML, essentially.