SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    how to replace one html element with a new made by js one

    i know how to remove an element:

    Code:
    var e = document.getElementById("the-element");
    e.parentNode.removeChild(e);
    and say i've already created a new html element called newE.

    how do you put newE in place of e ?

    thanks.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should do it:

    Code javascript:
    e.parentNode.insertBefore(e.nextSibling, newE);
    e.parentNode.removeChild(e);


  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ah right excellent. i'll give that a go. thanks.

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oldElement.parentNode.replaceChild(newElement,oldElement)

    returns oldElement, if you want to use it

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,292
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    that seems a much better way. the other way didn't work for me -- although i could easily have been doing something wrong. thanks.


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
  •