+ 2
It doesn't work in my computer
<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <button onClick = "hello()">click me</button> <div id="demo"></div> </body> </html> <script> function hello() { var p = document.createElement("button"); var node = document.createTextNode("new"); p.appendChild(node); var div = document.getElementById("demo"); div.appendChild(p); }; </script> i have been using this code in my computer but it doesn't work
5 Respuestas
+ 3
Your skeleton of your program is -
<html>
<head>...</head>
<body>...</body>
</html>
<script>...</script>
In above program <script> tag is outside body tag, it should be inside body tag, after <div>...</div>.
What it should be
<html>
<head>...</head>
<body>
<!--body code here>
<script>...</script>
</body>
</html>
and copy your new edited code to Notepad, and save it with ".html" or ".htm' extension. like myprogram.htm or hello.htm and open the file in a web browser like chrome or firefox.
Hope it helps.
+ 3
@Oleg <script> tag can be inside either <body> tag or <head> tag. Both are valid.
+ 3
@Uttam, ye i know, but i think put script in head tag it's better practice) it's just my mind)
0
thanks