Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Javascript appendchild executing twice Javascript appendchild executing twice

    Code:
    function displayResult()
    {
    var  damn = document.getElementById("textyar").value;
    document.getElementById("oneforty").innerHTML = damn;
    var newDIV = document.createElement('div');
    newDIV.className = "tweets";
    var newspan =document.createElement('span');
    newspan.className = "imagespanleft";
    var img = document.createElement("IMG");
    img.className="image";
    img.src = "tweets/me.jpg";
    newDIV.innerHTML = document.getElementById('textyar').value;  
    document.getElementById('leftmaindiv').appendChild(newDIV).appendChild(newspan).appendChild(img); 
    }

    this is the code , i m trying to create an interface like twitter where u put in text box and tweet appears below in
    problem is it is creating two divs instead of one , i tried insertbefore() as well but same behaviour
    plz ask any questions if u r still not clear

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Does it happen in all browsers or just in some?

    innerHTML doesn't always update the DOM properly when you use it and so trying to apply DOM methods to code where some of it has been updated that way doesn't always work. You might need to replace the second last line with creating a textNode to contain the value and appendChild that to the div instead of using innerHTML.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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