1

I have this code code.

It's work in jsfiddle but the problem when I test it in my file it's not working.

I have only displayed the first three lines created by html :

test h1

test h2

test p

(I test the code in different browsers and javascript is work but the same problem).

Code :

    var div2= document.createElement('div');  
    div2.className = "div2" ;
    var h1 = document.createElement('h1');
    h1.innerHTML= "test h1" ;
    var h2 = document.createElement('h2');
    h2.innerHTML= "test h2" ;
    var p = document.createElement('P');
    p.innerHTML= "p2" ;            
    div2.appendChild(h1);
    div2.appendChild(h2);
    div2.appendChild(p);
   document.getElementById('div1').appendChild(div2);

Thank you. ​

3
  • 2
    Like jsFiddle is, are you waiting for the document to load before attempting to append to div1? Commented Nov 5, 2012 at 17:06
  • 2
    @Sushanth-- Just setting it to jQuery and/or Pure javascript isn't the fix for the issue... OP needs to run it after the DOM is ready. Commented Nov 5, 2012 at 17:14
  • 1
    @Sushanth-- Why does the framework need to be changed? That has nothing to do with it Commented Nov 5, 2012 at 17:15

2 Answers 2

3

It's likely you are not waiting for the Document to load before running this code in your own application. Try wrapping your code in this block.

window.onload = function() {
    [your code here]
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem may have to do with when you're running your script. In your jsFiddle, you saved with the project defaults (which mean that your code is being called during the onLoad event, and with the MooTools library included).

If I switch your project to No Wrap (head) and No-Library (pure JS), the code stops working.

The reason is that you are attempting to do the work with the DOM before it's fully loaded into memory. You need to either run your script at the bottom of your page (which you can simulate in jsFiddle by changing the drop down to No Wrap (body)), or by running if after the onLoad event is fired in the browser:

window.onload = function(){
        var div2= document.createElement('div');  
        div2.className = "div2" ;
        var h1 = document.createElement('h1');
        h1.innerHTML= "test h1" ;
        var h2 = document.createElement('h2');
        h2.innerHTML= "test h2" ;
        var p = document.createElement('P');
        p.innerHTML= "p2" ;            
        div2.appendChild(h1);
        div2.appendChild(h2);
        div2.appendChild(p);
       document.getElementById('div1').appendChild(div2);
};

That will run it when the onLoad event has been fired, when the DOM is ready for access.

See this fiddle: http://jsfiddle.net/mori57/jzBpK/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.