<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sleepiness in Seattle &#187; dom</title>
	<atom:link href="http://stanblog.jojoyao.com/tag/dom/feed/" rel="self" type="application/rss+xml" />
	<link>http://stanblog.jojoyao.com</link>
	<description>Stanley&#039;s Chat on the Technology and Business</description>
	<lastBuildDate>Tue, 21 Apr 2009 04:29:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>innerHTML could invalidate the event handlers of its child elements</title>
		<link>http://stanblog.jojoyao.com/2007/05/innerhtml-could-invalidate-the-event-handlers-of-its-child-elements/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=innerhtml-could-invalidate-the-event-handlers-of-its-child-elements</link>
		<comments>http://stanblog.jojoyao.com/2007/05/innerhtml-could-invalidate-the-event-handlers-of-its-child-elements/#comments</comments>
		<pubDate>Fri, 11 May 2007 04:48:10 +0000</pubDate>
		<dc:creator>Stanley Yao</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://stanblog.jojoyao.com/newblog/?p=40</guid>
		<description><![CDATA[
]]></description>
			<content:encoded><![CDATA[<p>In Javascript, using an element&#8217;s innerHTML property to dynamically edit its content provides much flexibility and convenience.&nbsp; But special care needs to be taken when using it, because it could bite you silently.&nbsp; Take one of my experiences as an example, it could break the event handlers of the child elements.&nbsp; Sample code as the following:</p>
<table width="90%" cellspacing="1" cellpadding="1" border="1" align="center" summary="">
<tbody>
<tr>
<td>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;<br />
&lt;head&gt;<br />
&nbsp;&nbsp;&nbsp; &lt;title&gt;innerHTML&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;</p>
<p>&lt;hr /&gt;&lt;div id=&quot;myd&quot;&gt;&lt;/div&gt;&lt;hr /&gt;</p>
<p>&lt;script language=&quot;javascript&quot;&gt;<br />
function myaction()<br />
{<br />
&nbsp;&nbsp;&nbsp; alert(&quot;hello&quot;);<br />
}</p>
<p>var d1 = document.createElement(&quot;div&quot;);<br />
d1.onclick = myaction;<br />
d1.innerHTML = &quot;child div&quot;;</p>
<p>var d = document.getElementById(&quot;myd&quot;);<br />
d.appendChild(d1);<br />
//d.innerHTML += &quot;parent div&quot;;<br />
&lt;/script&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;</td>
</tr>
</tbody>
</table>
<p>
This code works well and when you click on div, &quot;hello&quot; will pop up.&nbsp; However if you uncomment the last line of the JavaScript code.&nbsp; The message won&#8217;t pop up any more.&nbsp; innerHTML manipulates the content of the element as a string instead of DOM, so it might just break the underlying &quot;wires&quot; that hooks up the elements and their event handlers.&nbsp; So watch out, when any of the events stops working as expected, look for the evil &quot;innerHTML&quot; first.</p>
]]></content:encoded>
			<wfw:commentRss>http://stanblog.jojoyao.com/2007/05/innerhtml-could-invalidate-the-event-handlers-of-its-child-elements/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

