<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Manipulating innerHTML removes events</title>
	<atom:link href="http://friendlybit.com/js/manipulating-innerhtml-removes-events/feed/" rel="self" type="application/rss+xml" />
	<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/</link>
	<description>Strategic web development</description>
	<lastBuildDate>Tue, 08 May 2012 22:48:12 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: wongkachun</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-34332</link>
		<dc:creator>wongkachun</dc:creator>
		<pubDate>Wed, 10 Nov 2010 05:13:49 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-34332</guid>
		<description>Dear All,

In conclusion, the innerHTML, DOM and html methods are used in following codes to show the event differences:




events of innerHtml and DOM dynamic html


    function changeinnerhtml(_id)
    {
        var obj = document.getElementById(_id);
        obj.onclick = function(){alert(_id + &#039; is clicked!&#039;)};
        var objparent = document.getElementById(&#039;para1&#039;);
        objparent.innerHTML += &#039; &lt;a href=&quot;http:\/\/yahoo.com.hk&quot; rel=&quot;nofollow&quot;&gt;YAHOO click me!&lt;/a&gt;&#039;;    
    }
    function changedom(_id)
    {
        var obj = document.getElementById(_id);
        obj.onclick = function(){alert(_id + &#039; is clicked!&#039;)};
        var objparent = document.getElementById(&#039;para1&#039;);
        var objnew = document.createElement(&#039;a&#039;);
        objnew.setAttribute(&quot;href&quot;,&quot;http://google.com.hk&quot;);
        objnew.appendChild(document.createTextNode(&quot;GOOGLE click me!&quot;));
        obj.appendChild(objnew);        
    }





Compare about event handlers of innerHTML, DOM and static html
innerHTML here
DOM here
hard coded html here

(1)When you click &quot;innerHTML here&quot;, no alert because parent tag (&amp;lt p &amp;gt) changed by innerHTML destroys the event.
(2)When you click &quot;DOM here&quot;, alert prompts dom tree is built within parent tag (&amp;lt p &amp;gt).
(3)When you click &quot;hard coded html here&quot;, alert should prompt because no structure changing within parent tag (&amp;lt p &amp;gt).
    
    changeinnerhtml(&#039;span1&#039;);
    changedom(&#039;span2&#039;);





Regards,
wongkachun</description>
		<content:encoded><![CDATA[<p>Dear All,</p>
<p>In conclusion, the innerHTML, DOM and html methods are used in following codes to show the event differences:</p>
<p>events of innerHtml and DOM dynamic html</p>
<p>    function changeinnerhtml(_id)<br />
    {<br />
        var obj = document.getElementById(_id);<br />
        obj.onclick = function(){alert(_id + &#8216; is clicked!&#8217;)};<br />
        var objparent = document.getElementById(&#8216;para1&#8242;);<br />
        objparent.innerHTML += &#8216; <a href="http:\/\/yahoo.com.hk" rel="nofollow">YAHOO click me!</a>&#8216;;<br />
    }<br />
    function changedom(_id)<br />
    {<br />
        var obj = document.getElementById(_id);<br />
        obj.onclick = function(){alert(_id + &#8216; is clicked!&#8217;)};<br />
        var objparent = document.getElementById(&#8216;para1&#8242;);<br />
        var objnew = document.createElement(&#8216;a&#8217;);<br />
        objnew.setAttribute(&#8220;href&#8221;,&#8221;http://google.com.hk&#8221;);<br />
        objnew.appendChild(document.createTextNode(&#8220;GOOGLE click me!&#8221;));<br />
        obj.appendChild(objnew);<br />
    }</p>
<p>Compare about event handlers of innerHTML, DOM and static html<br />
innerHTML here<br />
DOM here<br />
hard coded html here</p>
<p>(1)When you click &#8220;innerHTML here&#8221;, no alert because parent tag (&amp;lt p &amp;gt) changed by innerHTML destroys the event.<br />
(2)When you click &#8220;DOM here&#8221;, alert prompts dom tree is built within parent tag (&amp;lt p &amp;gt).<br />
(3)When you click &#8220;hard coded html here&#8221;, alert should prompt because no structure changing within parent tag (&amp;lt p &amp;gt).</p>
<p>    changeinnerhtml(&#8216;span1&#8242;);<br />
    changedom(&#8216;span2&#8242;);</p>
<p>Regards,<br />
wongkachun</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wongkachun</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-34331</link>
		<dc:creator>wongkachun</dc:creator>
		<pubDate>Wed, 10 Nov 2010 04:08:04 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-34331</guid>
		<description>Dear all,

I try to use div instead of span on the above coding.
The result is that the event of div is kept while span&#039;s is &quot;killed&quot;.

kachun</description>
		<content:encoded><![CDATA[<p>Dear all,</p>
<p>I try to use div instead of span on the above coding.<br />
The result is that the event of div is kept while span&#8217;s is &#8220;killed&#8221;.</p>
<p>kachun</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zcorpan</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-30057</link>
		<dc:creator>zcorpan</dc:creator>
		<pubDate>Tue, 01 Jan 2008 23:01:44 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-30057</guid>
		<description>Another way around this problem is to create your own appendHTML() method like so:

HTMLElement.prototype.appendHTML = function(s) {
  var div = document.createElement(&#039;div&#039;);
  div.innerHTML = s;
  while (div.firstChild)
    this.appendChild(div.firstChild);
}

This does what you thought &quot;innerHTML +=&quot; did (except that script blocks inserted this way will be executed).</description>
		<content:encoded><![CDATA[<p>Another way around this problem is to create your own appendHTML() method like so:</p>
<p>HTMLElement.prototype.appendHTML = function(s) {<br />
  var div = document.createElement(&#8216;div&#8217;);<br />
  div.innerHTML = s;<br />
  while (div.firstChild)<br />
    this.appendChild(div.firstChild);<br />
}</p>
<p>This does what you thought &#8220;innerHTML +=&#8221; did (except that script blocks inserted this way will be executed).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AdvancED DOM Scripting &#187; Out from under the carpet</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28356</link>
		<dc:creator>AdvancED DOM Scripting &#187; Out from under the carpet</dc:creator>
		<pubDate>Tue, 16 Oct 2007 01:07:33 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28356</guid>
		<description>[...] Bit had a post about events disappearing after using innerHTML to modify the DOM. When you add events using the [...]</description>
		<content:encoded><![CDATA[<p>[...] Bit had a post about events disappearing after using innerHTML to modify the DOM. When you add events using the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emil Stenström</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28178</link>
		<dc:creator>Emil Stenström</dc:creator>
		<pubDate>Thu, 04 Oct 2007 20:19:34 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28178</guid>
		<description>@Anders Ringqvist: Very interesting technique, thanks for linking! If I understand it correctly it&#039;s simply setting a listener on the outermost element, and waiting for the event to bubble there. Good way to avoid this problem.</description>
		<content:encoded><![CDATA[<p>@Anders Ringqvist: Very interesting technique, thanks for linking! If I understand it correctly it&#8217;s simply setting a listener on the outermost element, and waiting for the event to bubble there. Good way to avoid this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anders Ringqvist</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28159</link>
		<dc:creator>Anders Ringqvist</dc:creator>
		<pubDate>Wed, 03 Oct 2007 13:22:01 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-28159</guid>
		<description>A viable soultion could be to use event delegation instead of ”classic” event handling.

I let Christian Heilmann speak:

http://icant.co.uk/sandbox/eventdelegation/</description>
		<content:encoded><![CDATA[<p>A viable soultion could be to use event delegation instead of ”classic” event handling.</p>
<p>I let Christian Heilmann speak:</p>
<p><a href="http://icant.co.uk/sandbox/eventdelegation/" rel="nofollow">http://icant.co.uk/sandbox/eventdelegation/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emil Stenström</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27958</link>
		<dc:creator>Emil Stenström</dc:creator>
		<pubDate>Fri, 28 Sep 2007 19:55:56 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27958</guid>
		<description>@Jose Carrero: Many believe (including me) that adding things inline in the code like that is littering. You attach behaviour into the structure of the document. JS files should add their events from an external file, with onclick or something fancier.</description>
		<content:encoded><![CDATA[<p>@Jose Carrero: Many believe (including me) that adding things inline in the code like that is littering. You attach behaviour into the structure of the document. JS files should add their events from an external file, with onclick or something fancier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Nyman</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27925</link>
		<dc:creator>Robert Nyman</dc:creator>
		<pubDate>Fri, 28 Sep 2007 13:59:47 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27925</guid>
		<description>Freddy,

Thanks for explaining that! It threw us off for a while, so we just needed to think in alternate terms. In this case it was an AJAX call that could return any kind of HTML code, so using proper DOM methods wasn&#039;t a viable option.</description>
		<content:encoded><![CDATA[<p>Freddy,</p>
<p>Thanks for explaining that! It threw us off for a while, so we just needed to think in alternate terms. In this case it was an AJAX call that could return any kind of HTML code, so using proper DOM methods wasn&#8217;t a viable option.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ara Pehlivanian</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27924</link>
		<dc:creator>Ara Pehlivanian</dc:creator>
		<pubDate>Fri, 28 Sep 2007 13:54:30 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27924</guid>
		<description>Yeah, I came across this problem when writing an Ajax driven playlist for a project here at work. I was adding the new contents to the DOM with innerHTML and had to rebind all of the events for the entire list every time. Using DOM methods though will avoid this problem.</description>
		<content:encoded><![CDATA[<p>Yeah, I came across this problem when writing an Ajax driven playlist for a project here at work. I was adding the new contents to the DOM with innerHTML and had to rebind all of the events for the entire list every time. Using DOM methods though will avoid this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jose Carrero</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27881</link>
		<dc:creator>Jose Carrero</dc:creator>
		<pubDate>Thu, 27 Sep 2007 18:13:10 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27881</guid>
		<description>Maybe i&#039;m missing something but why add the onClick event using span.onclick=function()?, if you put the &quot;onClick&quot; inside the HTML tag it works fine even after the innerHTML change</description>
		<content:encoded><![CDATA[<p>Maybe i&#8217;m missing something but why add the onClick event using span.onclick=function()?, if you put the &#8220;onClick&#8221; inside the HTML tag it works fine even after the innerHTML change</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kalle Persson</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27879</link>
		<dc:creator>Kalle Persson</dc:creator>
		<pubDate>Thu, 27 Sep 2007 17:38:51 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27879</guid>
		<description>Nice, I did not know this.

Johan Lind:
Look at the source of the example page, the JavaScript isn&#039;t &lt;em&gt;supposed to&lt;/em&gt; do anything when you click the span, because of the innerHTML change.</description>
		<content:encoded><![CDATA[<p>Nice, I did not know this.</p>
<p>Johan Lind:<br />
Look at the source of the example page, the JavaScript isn&#8217;t <em>supposed to</em> do anything when you click the span, because of the innerHTML change.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Johan Lind</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27878</link>
		<dc:creator>Johan Lind</dc:creator>
		<pubDate>Thu, 27 Sep 2007 16:44:38 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27878</guid>
		<description>Hmm, nothing happens when I click on any of those elements in Safari and Camino.
Is this an IE only thing?</description>
		<content:encoded><![CDATA[<p>Hmm, nothing happens when I click on any of those elements in Safari and Camino.<br />
Is this an IE only thing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emil Stenström</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27876</link>
		<dc:creator>Emil Stenström</dc:creator>
		<pubDate>Thu, 27 Sep 2007 16:19:20 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27876</guid>
		<description>@Freddy: Ah, thanks. It makes even more sense then.</description>
		<content:encoded><![CDATA[<p>@Freddy: Ah, thanks. It makes even more sense then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Freddy</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27871</link>
		<dc:creator>Freddy</dc:creator>
		<pubDate>Thu, 27 Sep 2007 13:17:25 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27871</guid>
		<description>@Emil: I didn&#039;t want to sound elitist, sorry if it came across as such. JFTR: In JavaScript, there is no &quot;adding to&quot; strings; Adding using the unary + (&quot;+=&quot;) is just a &quot;shorthand&quot; for reading, adding, and reassigning values (internally, they convert number/string types, but that&#039;s not relevant in this context).</description>
		<content:encoded><![CDATA[<p>@Emil: I didn&#8217;t want to sound elitist, sorry if it came across as such. JFTR: In JavaScript, there is no &#8220;adding to&#8221; strings; Adding using the unary + (&#8220;+=&#8221;) is just a &#8220;shorthand&#8221; for reading, adding, and reassigning values (internally, they convert number/string types, but that&#8217;s not relevant in this context).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Emil Stenström</title>
		<link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27870</link>
		<dc:creator>Emil Stenström</dc:creator>
		<pubDate>Thu, 27 Sep 2007 13:11:20 +0000</pubDate>
		<guid isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27870</guid>
		<description>@Freddy: Good that you found it obvious, I just had to think for a while before it came to me. I thought I might save someone&#039;s time by stating it, but deeming by your tone I might have been wrong about that.

As you said, the confusing part here is that adding to and replacing innerHTML is the same. 

Thanks for commenting.</description>
		<content:encoded><![CDATA[<p>@Freddy: Good that you found it obvious, I just had to think for a while before it came to me. I thought I might save someone&#8217;s time by stating it, but deeming by your tone I might have been wrong about that.</p>
<p>As you said, the confusing part here is that adding to and replacing innerHTML is the same. </p>
<p>Thanks for commenting.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/8 queries in 0.001 seconds using disk: basic
Object Caching 385/386 objects using disk: basic

Served from: friendlybit.com @ 2012-05-22 09:54:41 -->
