SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member HaloprO's Avatar
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question object.appendChild not working

    Howdy, this is my first time asking for JavaScript help. I searched for a while, but I could not find anything.
    Code:
    tmp = document.createElement('div');
    tmp.setAttribute('id', 'caption' + captions);
    tmp.setAttribute('class', 'caption');
    txt = document.createTextNode('My Caption!');
    tmp.appendChild(txt);
    image.appendChild(tmp);
    image is a div element in the body, and captions is an integer
    I recieve no error when the script executes, and it reaches this part of the script because I've alerted tmp and image, they both read as [Object HTMLDivElement]
    Any ideas?

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to append the image element to the body before it will display. Try:

    document.body.appendChild(image);

  3. #3
    SitePoint Member HaloprO's Avatar
    Join Date
    Dec 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if the image is already an element visible in the body?

    Edit, that worked but it puts it to the end of the body. I suppose I can insertBefore and remove the old one..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •