Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!news.weisnix.org!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <5848329.bgypaU67uL@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 12 Jan 2012 18:32:46 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: Cannot write to the DOM Newsgroups: comp.lang.javascript References: <3437926.47.1326345632135.JavaMail.geo-discussion-forums@yqiq10> <4f0ee77c$0$30355$a8266bb1@newsreader.readnews.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 39 NNTP-Posting-Date: 12 Jan 2012 18:32:46 CET NNTP-Posting-Host: 1581af43.newsspool1.arcor-online.net X-Trace: DXC=\`5^JZ02af]mG86`U=_nC_ic==]BZ:af^4Fo<]lROoRQ<`=YMgDjhgRE4CgK@@nP?\DZm8W4\YJN\;?f@h5gMfb\Q>]g3QkX_`Th7h2LVHR=7[ X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:10179 Denis McMahon wrote: > On Wed, 11 Jan 2012 21:20:32 -0800, RH wrote: >> function create() { >> // create the element in the DOM >> var listItemViewer = document.getElementById("test"); >> >> var test = document.createElement("li"); test.innerHTML = "test list >> item"; >> listItemViewer.appendChild(test); >> } > > What the others said, but in addition, maybe this is a better create(): > > function create() { > var listItemViewer = document.getElementById("test"); > var test = document.createElement("li"); > test.appendChild(document.createTextNode("test list item")); > listItemViewer.appendChild(test); > } > > If you're going to use dom functions to create the list element, you > might as well (some might even suggest should) also use the dom functions > to add its content instead of innerHTML. It is inefficient to retrieve the element object reference on every click. So you should cache that somehow. However, if you are using this function then you must by definition have either an empty `li' element in the document already (which is not modified by this code, which would be a rather bad idea) or you must create the `ul' element dynamically, in which case you have the element object reference already (as I showed). PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann