I’m learning to write with html, but I have one problem I cannot understand.
The following code works in other browsers but not in IE9. I know it has something to do with innerHTML but I could not understand the answers I found for this.
<html> <head> <script type="text/javascript">
function hw_function1() {
var img11=document.createElement("a");
img11.innerHTML = "<html> <body> <a href='http://google.de'>Google</a> </body></html>";
document.body.appendChild( img11 );
}
</script>
<body>
<a href="#" onclick="javascript:hw_function1()";>Test</a>
</body> </html>
What should I change WITHOUT changing the structure (only the innerHTMl-part if possible)?
Since you’re creating an
aelement, you simply assign the href to the element via the.hrefproperty.You can set its text content with
.innerHTMLas a convenience, though it’s not really a “pure” approach.Another way to set the text content would be like this…