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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2017
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    document.createElement not working - complete code pasted here

    Based on the code below, "Hi there and greetings" text isn't displayed on the page.


    <!DOCTYPE html>
    <html>


    <head>
    <title>||Working with elements||</title>
    </head>
    <body onload="alert('body.onload is working')">
    <script LANGUAGE = "Javascript">
    function addElement () {
    // create a new div element
    // and give it some content
    var newDiv = document.createElement("div");
    var newContent = document.createTextNode("Hi there and greetings!");
    newDiv.appendChild(newContent); //add the text node to the newly created div.

    // add the newly created element and its content into the DOM

    var currentDiv = document.getElementbyId("div1");
    document.body.insertBefore(newDiv, currentDiv);
    }
    </script>
    <div id="div1">The text above has been created dynamically.
    <P>
    <INPUT TYPE="Button" Value="Send" onClick="addElement()">
    </P>
    </div>
    </body>
    </html>

  2. #2
    Regular Coder
    Join Date
    Sep 2010
    Location
    U S of A
    Posts
    206
    Thanks
    4
    Thanked 39 Times in 39 Posts
    var currentDiv = document.getElementbyId("div1");//getElementById

  3. Users who have thanked Shaka Zorba for this post:

    bishweshwar (01-10-2017)

  4. #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,620
    Thanks
    58
    Thanked 662 Times in 657 Posts
    Quote Originally Posted by bishweshwar View Post
    <!DOCTYPE html>
    <html>


    <head>
    <title>||Working with elements||</title>
    </head>
    <body onload="alert('body.onload is working')">
    <script LANGUAGE = "Javascript">
    function addElement () {
    // create a new div element
    // and give it some content
    var newDiv = document.createElement("div");
    var newContent = document.createTextNode("Hi there and greetings!");
    newDiv.appendChild(newContent); //add the text node to the newly created div.

    // add the newly created element and its content into the DOM

    var currentDiv = document.getElementbyId("div1");
    document.body.insertBefore(newDiv, currentDiv);
    }
    </script>
    <div id="div1">The text above has been created dynamically.
    <P>
    <INPUT TYPE="Button" Value="Send" onClick="addElement()">
    </P>
    </div>
    </body>
    </html>
    I hope you're not paying to learn to code like that.

  5. #4
    New Coder
    Join Date
    Jan 2017
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    I hope you're not paying to learn to code like that.
    I am at a beginner stage and learning on my own. You can guide me on correct path so I don't have to pay.

  6. #5
    New Coder
    Join Date
    Jan 2017
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Shaka Zorba View Post
    var currentDiv = document.getElementbyId("div1");//getElementById
    Thanks so much for figuring the typo. It worked!

  7. #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,620
    Thanks
    58
    Thanked 662 Times in 657 Posts
    Here is a very good set of tutorials that you should be reading instead of whatever you are using now: Introducing JavaScript

    This is probably the mosty definitive JavaScript reference: https://developer.mozilla.org/en-US/...vaScript/Guide

    Before reading any of that, though, you should learn how to use the error console in your browser.


 

Tags for this Thread

Posting Permissions

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