www.webdeveloper.com
Results 1 to 4 of 4

Thread: Newbe Question

  1. #1
    Join Date
    Jun 2011
    Posts
    2

    Newbe Question

    hi guys, as a beginner
    i just started to learn Javascript and i really can not follow one example of a book i use:

    with tho following code there suppose to be a paragraph inserted between the body tags. Here the file i have written:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Making Sums</title>
    <script type="text/javascript">
    var paRa= document.createElement("p");
    paRa.setAttribute("class", "intro");
    document.body.appendChild(paRa);
    var newText = document.createTextNode("This is waht i cant see!");
    paRa.appendChild(newText);
    </script>
    </head>
    <body>
    </body>
    </html>
    So far no code is inserted if i examine the source code...
    Any help?
    Thax a lot...

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    Adding HTML with JavaScript will not add it to the source code. It will add it to the DOM.

    I assume you see the paragraph right? Just not when you click "view source". If you want to see it in the DOM, you and use Firefox or Chrome, right click, and select "Inspect Element".
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Jun 2011
    Posts
    2
    thanx vision...i think the problem is the order of the js code...it suppose to be inserted after the body tags...but i really would prefer a function in the <head> of the script...so far im not ready to do that and read some more stuff on that ;-P

  4. #4
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    HTML Code:
    document.body.appendChild(paRa);
    must be after
    HTML Code:
    paRa.appendChild(newText);
    Your paragraph would be there, but without the text added; you've added the paragraph to the DOM tree without adding the text to it first. Another problem could be that you've got your script running before the body loads.
    Computer programmer / web developer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles