{"id":802,"date":"2006-05-30T18:27:52","date_gmt":"2006-05-30T22:27:52","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=802"},"modified":"2006-05-30T18:27:52","modified_gmt":"2006-05-30T22:27:52","slug":"cant-add-hyperlink-with-onclick-to-ie-using-dom","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2006\/05\/30\/cant-add-hyperlink-with-onclick-to-ie-using-dom\/","title":{"rendered":"Can&#8217;t add hyperlink with onclick to IE using DOM"},"content":{"rendered":"<p>Last week I was attempting to dynamically add hyperlinks that contained an <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/onclick\">onclick<\/a> event to a document. The hyperlink, when written to the browser, would look something like this:<br \/>\n<code><br \/>\n&lt;a href=\"#\" onclick=\"fireEvent(); return false;\"&gt;fire away!&lt;\/a&gt;<br \/>\n<\/code><br \/>\nand I was using code that looked something like this to produce the links (and yes, I know about <a href=\"http:\/\/diveintoaccessibility.org\/day_13_using_real_links.html\">real links<\/a>):<br \/>\n<code><br \/>\n&lt;script&gt;<br \/>\nfunction createLink(id) {<br \/>\n&nbsp;&nbsp;var node = document.getElementById(id);<br \/>\n&nbsp;&nbsp;var aNode = document.createElement(\"a\");<br \/>\n&nbsp;&nbsp;node.appendChild(aNode);<br \/>\n&nbsp;&nbsp;aNode.setAttribute(\"href\",\"#\");<br \/>\n&nbsp;&nbsp;aNode.setAttribute(\"onclick\", \"fireEvent(); return false;\");<br \/>\n&nbsp;&nbsp;aNode.appendChild(document.createTextNode(\"fire away!\"));<br \/>\n}<br \/>\nfunction fireEvent() {<br \/>\n&nbsp;&nbsp;alert('fire!');<br \/>\n}<br \/>\n&lt;\/script&gt;<br \/>\n&lt;a href=\"#\" onclick=\"createLink('mytable'); return false;\"&gt;create link&lt;\/a&gt;<br \/>\n&lt;table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"&gt;<br \/>\n&lt;tbody&gt;<br \/>\n&lt;tr&gt;<br \/>\n&nbsp;&nbsp;&lt;td id=\"mytable\"&gt;&lt;\/td&gt;<br \/>\n&lt;\/tr&gt;<br \/>\n&lt;\/tbody&gt;<br \/>\n&lt;\/table&gt;<br \/>\n<\/code><br \/>\nRunning the above code in Firefox worked fine, the fireEvent() method was invoked when you clicked the &#8216;fire away!&#8217; link.  The same exact code in IE didn&#8217;t work. Instead, it appeared that IE completely ignored the onclick event and just went with the value of the href attribute.  The simple workaround, in this case at least, was to use the <a href=\"http:\/\/developer.mozilla.org\/en\/docs\/DOM:element.innerHTML\">innerHTML <\/a>property of the cell I was adding the link to, so the createLink() function became:<br \/>\n<code><br \/>\nfunction createLink(id) {<br \/>\n&nbsp;&nbsp;var node = document.getElementById(id);<br \/>\n&nbsp;&nbsp;node.innerHTML = '&lt;a href=\"#\" onclick=\"fireEvent(); return false;\"&gt;fire away!&lt;\/a&gt;';<br \/>\n}<br \/>\n<\/code><br \/>\nAm I wrong in thinking that this is a bug? <\/p>\n<p>Related:<\/p>\n<p><a href=\"http:\/\/www.quirksmode.org\/js\/events_early.html\">Early event handlers<\/a><br \/>\n<a href=\"http:\/\/karma.nucleuscms.org\/item\/101\">Javascript: createElement, innerHTML and IE security zones<\/a><br \/>\n<a href=\"http:\/\/ncyoung.com\/entry\/494?PHPSESSID=d8992878f4648c5502afd5e88b96cd61\">can&#8217;t put a row into a table in IE using DOM methods<\/a><\/p>\n<p><font color=\"red\"><br \/>\nUpdated 7\/3\/2006: My boss Bill sent a note mentioned the <a href=\"http:\/\/www.ccs.neu.edu\/home\/dherman\/javascript\/behavior\/\">behaviors library<\/a> which uses AOP techniques to reduce what the author calls &#8216;scattering&#8217; and &#8216;tangling&#8217; and would may also be a solution to the problem mentioned above.  <\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last week I was attempting to dynamically add hyperlinks that contained an onclick event to a document. The hyperlink, when written to the browser, would look something like this: &lt;a href=&#8221;#&#8221; onclick=&#8221;fireEvent(); return false;&#8221;&gt;fire away!&lt;\/a&gt; and I was using code that looked something like this to produce the links (and yes, I know about real &hellip; <a href=\"https:\/\/cephas.net\/blog\/2006\/05\/30\/cant-add-hyperlink-with-onclick-to-ie-using-dom\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Can&#8217;t add hyperlink with onclick to IE using DOM<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[29],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/802"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}