I have a function that adds an anchor/hyperlink to an element with ID "title".
var a = document.createElement('a');
a.innerHTML = "Link text";
a.href = "http://example.com";
a.style.fontSize = "14px";
a.style.fontWeight = "normal";
a.onclick = function() { alert('OK'); };
// now add anchor to DOM
$('title').insertBefore(a);
The link is added successfully to the DOM and its style attributes are set just fine, but not the onclick
. Viz., this is what I get:
<a href="http://example.com" style="font-size: 14px; font-weight: normal;">Link text</a>
Why isn't the onclick
added? What am I doing wrong?