Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15371
| 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> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| 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+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Subject | Re: How to create nested elements with createElement(...)? |
| Newsgroups | comp.lang.javascript |
| References | <k0bng9$8it$1@news.albasani.net> <t5CdnSnEKeJG8bTNnZ2dnUVZ_jSdnZ2d@westnet.com.au> |
| 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<OcfhCOkdd@C8U4BnnnDZm8W4\YJNlR=i2=[N6Y6j^4o1HaKY5DlmIJDTl4nhFl |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:15371 |
Followups directed to: comp.lang.javascript
Show key headers only | View raw
Andrew Poulos wrote:
> 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 <div>, 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 += '<div><img src="image.png"></div>'
*/
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<input type="checkbox" name="bar" value="baz">'¹
*/
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]
<http://PointedEars.de/websvn/filedetails.php?repname=JSX&path=/trunk/dom.js>
--
When all you know is jQuery, every problem looks $(olvable).
Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread
How to create nested elements with createElement(...)? Tuxedo <tuxedo@mailinator.com> - 2012-08-13 22:19 +0200
Re: How to create nested elements with createElement(...)? "Tom de Neef" <tdeneef@qolor.nl> - 2012-08-13 23:06 +0200
Re: How to create nested elements with createElement(...)? Andrew Poulos <ap_prog@hotmail.com> - 2012-08-14 07:08 +1000
Re: How to create nested elements with createElement(...)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-08-16 03:42 +0200
csiph-web