create link in javascript function with --- innerHTML = "...<a href='http://...'>HttpLink</a>..."

function, innerhtml, internet-explorer, javascript

Solution

Since you're creating an `a` element, you simply assign the href to the element via the `.href` property.

You can set its text content with `.innerHTML` as a convenience, though it's not really a "pure" approach.

var img11=document.createElement("a");

img11.href='http://google.de';
img11.innerHTML = 'Google';

document.body.appendChild( img11 );

Another way to set the text content would be like this...

img11.appendChild(document.createTextNode("Google"));

Problem

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)?

Original source