<?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>You have found Friendly Bit, a web development blog. I focus on client side technologies like CSS, HTML and Javascript. You find my articles below and categories to the right.</description> <lastBuildDate>Thu, 11 Mar 2010 16:54:48 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.9.2</generator> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <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> <item><title>By: Freddy</title><link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27869</link> <dc:creator>Freddy</dc:creator> <pubDate>Thu, 27 Sep 2007 10:55:00 +0000</pubDate> <guid
isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27869</guid> <description>I&#039;ll have to call &#039;duh&#039; on that one. Obviously, events on an element cannot be retained if the element is replaced by a completely different string using innerHTML. You basically throw away that part of the DOM and write it new. Don&#039;t have to tell you that.The only thing slightly noteworthy is that this also applies when using the + unary operator (&quot;+=&quot;). And given that it is required to call GetValue() and return the sum, which is in turn written back as a whole, it is not very surprising.</description> <content:encoded><![CDATA[<p>I&#8217;ll have to call &#8216;duh&#8217; on that one. Obviously, events on an element cannot be retained if the element is replaced by a completely different string using innerHTML. You basically throw away that part of the DOM and write it new. Don&#8217;t have to tell you that.</p><p>The only thing slightly noteworthy is that this also applies when using the + unary operator (&#8220;+=&#8221;). And given that it is required to call GetValue() and return the sum, which is in turn written back as a whole, it is not very surprising.</p> ]]></content:encoded> </item> <item><title>By: Emil Stenström</title><link>http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27867</link> <dc:creator>Emil Stenström</dc:creator> <pubDate>Thu, 27 Sep 2007 07:49:13 +0000</pubDate> <guid
isPermaLink="false">http://friendlybit.com/js/manipulating-innerhtml-removes-events/#comment-27867</guid> <description>@Nate: No, using DOM methods instead keeps the events as they should be. That&#039;s one workaround. The other one is to attach events &lt;strong&gt;after&lt;/strong&gt; using  innerHTML.</description> <content:encoded><![CDATA[<p>@Nate: No, using DOM methods instead keeps the events as they should be. That&#8217;s one workaround. The other one is to attach events <strong>after</strong> using  innerHTML.</p> ]]></content:encoded> </item> </channel> </rss>
<!-- This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 22/36 queries in 0.044 seconds using disk

Served from: c8.67.364a.static.theplanet.com @ 2010-03-12 13:15:12 -->