SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: DOM problems

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM problems

    Hi wonder if anyone can help. I have the following script.

    Code:
    <div id="gallery-viewer">
    <img src="some-image.jpg" alt="some image">
    </div>
    I have a UL list that when you click it adds a new image in according to what link you press this all works fine the problem I have is deleting the original image before appending the new one.

    Code:
    function init(e)
    	{
    		var DIV = document.getElementById("gallery-viewer")
    		// Delete Old Img First
    		var oldIMG = DIV.getElementsByTagName("IMG")
    		//oldIMG.parent.removeChild(oldIMG)
    		DIV.removeChild( oldIMG )
    
    
                    // this all works below no need to worry about this
    		var Sclass = this.href.split("#");
    		var IMG = document.createElement("IMG")
    		
    IMG.setAttribute("src", '<?php echo GBL_ADDRESS;>/_assets/client/images/casestudies/' + Sclass[1] + '.jpg')
    		IMG.setAttribute("alt", 'Case Study Image')
    		DIV.appendChild(IMG)
    
    		return false;	
    	}
    For some reason I cannot get it to delete the old img tag I have tried numerous methods but no joy I have the variables I need for the Old Image (the one I am deleting) and where it is place DIV but cant delete it

  2. #2
    Winemaster bronze trophy BonRouge's Avatar
    Join Date
    Oct 2004
    Location
    Sendai, Japan
    Posts
    2,417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    oldIMG.parentNode.removeChild(oldIMG)
    ?
    Edit:

    D'oh! I didn't look carefully enough.
    Last edited by BonRouge; Apr 8, 2007 at 19:27.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var oldIMG = DIV.getElementsByTagName("IMG")

    oldIMG is not an element, but a node list of elements.
    if there is only one member, use oldIMG[0] to reach the element.
    And instead of deleting and appending, use
    DIV.replaceChild(IMG,oldIMG[0])


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
  •