Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15352

Re: How to create nested elements with createElement(...)?

Date 2012-08-14 07:08 +1000
From Andrew Poulos <ap_prog@hotmail.com>
Newsgroups comp.lang.javascript
Subject Re: How to create nested elements with createElement(...)?
References <k0bng9$8it$1@news.albasani.net>
Message-ID <t5CdnSnEKeJG8bTNnZ2dnUVZ_jSdnZ2d@westnet.com.au> (permalink)

Show all headers | View raw


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);

Andrew Poulos

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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