-1
<!DOCTYPE>
<html>
<script>
    var btn = document.createElement("BUTTON");
    var txt = document.createTextNode("Text");
    btn.appendChild(txt);
    document.body.appendChild(btn);
</script>
</html>

I just started learning HTML and JavaScript.

I was expecting the above code to output a button with a word "text" in it. Unfortunately, the output was blank. Can someone explain to me why this code didn't work?

Constant
  • 780
  • 1
  • 5
  • 19
Ethyl Casin
  • 791
  • 2
  • 16
  • 34
  • There's no body to attach to – Sterling Archer Apr 17 '15 at 02:21
  • The javascript is trying to run before the page has loaded causing it to error. You should also have a body tag but that isn't the reason your button isn't being created. This does work without the body tag but i wouldn't recommend doing that. – NewToJS Apr 17 '15 at 02:21
  • Yeah your right! Theres an error: Uncaught TypeError: Cannot read property 'appendChild' of null – Ethyl Casin Apr 17 '15 at 02:24

3 Answers3

3

your script won't do anything because it in head so your script run before body you need use window.onload function like this

<!DOCTYPE>
<html>
<script>
window.onload = function(){
    var btn = document.createElement("BUTTON");
    var txt = document.createTextNode("Text");
    btn.appendChild(txt);
    document.body.appendChild(btn);
}
</script>
</html>
  • Will this append to the body even if there is no `` to append to? – Sumner Evans Apr 17 '15 at 02:26
  • Yes, it will append. I have tried this with and without the body tag. Not that i would recommend doing this without the body tag. The issue is the javascript running before the page is read. – NewToJS Apr 17 '15 at 02:27
  • look at that http://stackoverflow.com/questions/5641997/is-it-necessary-to-write-head-body-and-html-tags –  Apr 17 '15 at 02:29
  • @ethylcasin - IMO, a better solution would be to just move the ` – jfriend00 Apr 17 '15 at 02:54
  • yes , but she want it in head , whatever it works :) –  Apr 17 '15 at 02:58
1

jsve is close. You need to make sure the code isloaded when the body loads. This is done by calling the body's onload function, like this...

<!doctype html>
<html>
<head>
    <script>
    function init(){
        var btn = document.createElement("BUTTON");
        var txt = document.createTextNode("Text");
        btn.appendChild(txt);
        document.body.appendChild(btn);
    }
    </script>
</head>
<body onload="init()"></body>
</html>
James Grundner
  • 1,454
  • 1
  • 10
  • 12
0

You need to run that code after the body is loaded:

<!doctype html>
<html>
<head>
    <script>
        function addBtn() {
            var btn = document.createElement("button");
            var txt = document.createTextNode("Text");
            btn.appendChild(txt);
            document.body.appendChild(btn);
        }
    </script>
</head>
<body onload="addBtn()"></body>
</html>

A few more comments:

  1. You also need to update your DOCTYPE to <!doctype html> as I show above.

  2. Wrap your <script> tag inside of a <head> tag.

  3. No need to capitalize button.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • This won't work because the javascript is running before the page is read. Try `window.onload=function(){*place the javascript here*}` – NewToJS Apr 17 '15 at 02:24
  • @jsve I see it. I have also given another method in the comment. – NewToJS Apr 17 '15 at 02:26