Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <2631782.XQDAum8ly4@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Thu, 16 Aug 2012 03:42:47 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 94 NNTP-Posting-Date: 16 Aug 2012 03:42:47 CEST NNTP-Posting-Host: 7772799a.newsspool3.arcor-online.net X-Trace: DXC=]AZ[B;N?HeheoCI^f\Y]EaMcF=Q^Z^V3h4Fo<]lROoRa8kF On 14/08/2012 6:19 AM, Tuxedo wrote: >> This appends an image to the body: >> >> function appendElement(){ >> var img = document.createElement("img"); >> img.src = 'image.png'; >> document.body.appendChild(img); >> } >> >> How can the same function be made also to create a
, so that the div >> contains the image, or for a div to be appended to the body and for the >> image to be appended to appear within the div? > > This would be one way: > > var mydiv = document.createElement("div"); > var myimg = document.createElement("img"); > myimg.src = "image.png"; > mydiv.appendChild(myimg); > document.body.appendChild(mydiv); As you need that frequently when avoiding .innerHTML, I have written jsx.dom.createElementFromObj() [1]: /* * Equivalent of * document.body.innerHTML += '
' */ var mydiv = jsx.dom.createElementFromObj({ type: "div", childNodes: [ { type: "img", properties: { src: "image.png" } } ] }); document.body.appendChild(mydiv); Being recursive, it already supported the creation of a text node (through a string-typed object as argument), so I added jsx.dom.createNodeFromObj() as alias yesterday. Also, out of necessity in a commercial project, I tweaked it so that it would return a reference to an Array (heretoafter: an Array) of Nodes when passed an Array of string-typed values (for text nodes) or other objects (for element nodes). As I result, I added the alias jsx.dom.createNodesFromObj(), and jsx.dom.appendChildren() and jsx.dom.removeChildren(), which make filling and emptying an element node easy: /* * Equivalent of * parentNode.innerHTML += * 'foo'¹ */ jsx.dom.appendChildren(parentNode, jsx.dom.createNodesFromObj([ "foo", { type: "input", properties: { type: "checkbox", name: "bar", value: "baz" } } ])); // … /* Equivalent of parentNode.innerHTML = "" */ jsx.dom.removeChildren(parentNode, parentNode.childNodes); Now it strikes me that jsx.dom.appendChildren() should return an Array of Nodes created from the passed Array, and jsx.dom.removeChildren() should return an Array of removed Nodes. The methods are not yet namespace-aware. What do you think? _______ ¹ Note that an element object's properties are _not_ equivalent to that element's attributes, although it may appear so in these examples. [1] -- When all you know is jQuery, every problem looks $(olvable).