SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    appendChild / removeChild swapping images on the fly

    I have an image inside of a div that I'd like to swap every time the function is triggered. I was able to attach first image through this:

    Code JavaScript:
    this.titlecontainer.appendChild(document.createElement('img')).src = 'images/picture' + padded_index + '-t.png';

    This indeed does add another image on each function iteration but as the code suggests, it actually APPENDS additional images. Instead, I'd like to SWAP the existing image for a new one. My guess is to remove what's currently loaded inside the div prior to appending another one. What would the line preceding the above have to look like to accomplish removing of the content?

    Many thanks

  2. #2
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Instead of appending a new child element, just change the src of the existing img.

    Code:
    document.getElementById('myimg').src = 'images/picture' + padded_index + '-t.png';
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Clark


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
  •