<?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>alt-tag.com &#187; Javascript</title>
	<atom:link href="http://alt-tag.com/blog/archives/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://alt-tag.com/blog</link>
	<description>Thoughts on everything from education and politics to internet usability, and programming.</description>
	<lastBuildDate>Sun, 22 Nov 2009 17:52:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>IE DOM Bugs</title>
		<link>http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/</link>
		<comments>http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/#comments</comments>
		<pubDate>Wed, 08 Feb 2006 05:40:41 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Standards]]></category>

		<guid isPermaLink="false">http://tag-strategia.com/blog/?p=31</guid>
		<description><![CDATA[I&#8217;ve been working on a Javascript project where it&#8217;s necessary to create input elements (radio buttons and checkboxes) dynamically. With a functional DOM, it takes only a couple of lines of code, and works fine in Firefox and Safari. Too bad IE isn&#8217;t as DOM compatible as it claims to be.
After several searches, I discovered [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a Javascript project where it&#8217;s necessary to create <code>input</code> elements (radio buttons and checkboxes) dynamically. With a functional <abbr title="Document Object Model">DOM</abbr>, it takes only a couple of lines of code, and works fine in Firefox and Safari. Too bad IE isn&#8217;t as DOM compatible as it claims to be.</p>
<p>After several searches, I discovered <a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp" title="MSDN Reference: name attribute">IE doesn&#8217;t allow the <code>name</code> attribute to be changed</a> after the element is created&#8212;and it can&#8217;t be set in a DOM compatible way during creation.</p>
<p>Bennett McElwee <a href="http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/" title="Semicolon: Setting the &quot;name&quot; attribute in Internet Explorer">suggested a solution in his blog</a> that is nicely cross-browser; anything other than IE throws an exception and gets created properly. (I suspect modifying the parent node&#8217;s <code>innerHTML</code> would work as well.)</p>
<p><code class="codeblock"><span class="keyword">function</span> <span class="function">createElement</span>(type, name) {<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">var</span> element = <span class="keyword">null</span>;<br />
<br />&nbsp;&nbsp;&nbsp;<span class="keyword">try</span> {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">// First try the IE way; if this fails then use the standard way</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element = <span class="keyword">document</span>.<span class="function">createElement</span>(<span class="string">&#39;&lt;&#39;</span>+type+<span class="string">&#39; name=&quot;&#39;</span>+name+<span class="string">&#39;&quot;>&#39;</span>);<br />
&nbsp;&nbsp;&nbsp;} <span class="keyword">catch</span> (e) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">// Probably failed because we're not running on IE</span><br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">if</span> (!element) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element = <span class="keyword">document</span>.<span class="function">createElement</span>(type);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.name = name;<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">return</span> element;<br />
}</code></p>
<h3>And your <code>type</code> attribute too</h3>
<p>Another part of the project requires we transform checkboxes to radio buttons and hidden fields. This could be accomplished through a page reload, but it&#8217;s overkill for such a small change. Once again, in truly DOM-compliant browsers, this requires only a couple of lines of code:</p>
<p><code class="codeblock">element.<span class="function">setAttribute</span>(<span class="string">&quot;type&quot;</span>, <span class="string">&quot;radio&quot;</span>);</code></p>
<p>The <a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp" title="MSDN Reference: name attribute">MSDN reference for the <code>type</code> attribute</a> says:</p>
<blockquote cite="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp"><p>As of Microsoft Internet Explorer 5, the <code>type</code> property is read/write-once, but only when an <code>input</code> element is created with the <code>createElement</code> method and before it is added to the document.</p></blockquote>
<p><a href="http://www.quirksmode.org/home.shtml" title="QuirksMode.org">QuirksMode</a> has a <a href="http://www.quirksmode.org/bugreports/archives/2006/01/Changing_the_type_of_an_input_field.html">bug report</a> for this, complete with <a href="http://stijn.gamerzheaven.nl/tests/JS2.htm">test page</a> and workaround submitted by Stijn Peeters.  Stijn admits the workaround needs a little bit of cleanup.</p>
<p>Essentially, his solution is to always remove the element, and recreate a modified one. (See the bug above!) Here&#8217;s my solution:</p>
<p><code class="codeblock"><span class="keyword">try</span> {<br />
&nbsp;&nbsp;&nbsp;element.<span class="function">setAttribute</span>(<span class="string">&quot;type&quot;</span>, <span class="string">&quot;radio&quot;</span>);<br />
} <span class="keyword">catch</span> (e) {<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">var</span> newElement = <span class="keyword">null</span>;<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">var</span> tempStr = element.<span class="function">getAttribute</span>(<span class="string">&quot;name&quot;</span>);<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">try</span> {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newElement = <span class="keyword">document</span>.<span class="function">createElement</span>(<span class="string">&quot;&lt;input type=\&quot;&quot;</span> +typeStr+ <span class="string">&quot;\&quot; name=\&quot;&quot;</span> +tempStr+ <span class="string">&quot;\&quot;>&quot;</span>);<br />
&nbsp;&nbsp;&nbsp;} <span class="keyword">catch</span> (e) {}<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">if</span> (!newElement) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newElement = <span class="keyword">document</span>.<span class="function">createElement</span>(<span class="string">&quot;input&quot;</span>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newElement.<span class="function">setAttribute</span>(<span class="string">&quot;type&quot;</span>, <span class="string">&quot;radio&quot;</span>);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newElement.<span class="function">setAttribute</span>(<span class="string">&quot;name&quot;</span>, tempStr);<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;<span class="keyword">if</span> (tempStr = element.<span class="function">getAttribute</span>(<span class="string">&quot;value&quot;</span>)) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newElement.<span class="function">setAttribute</span>(<span class="string">&quot;value&quot;</span>, tempStr);<br />
&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;element.parentNode.<span class="function">replaceChild</span>(newElement, element);<br />
}</code></p>
<div class="seeAlso">
<h3>Update:</h3>
<p>Aaron over at easy-reader.net <a href="http://www.easy-reader.net/archives/2005/09/02/death-to-bad-dom-implementations/" title="easy-reader.net: Death to bad DOM implementations">encountered the same problem</a> a few months before I did. His solution is similar, and the comments there are good. If only I had found it sooner!</div>
]]></content:encoded>
			<wfw:commentRss>http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
