i have some html and javascript, but the javascript only executes when I put it in an onclick. i am trying to just create a new list element.
dosent work:
<html>
<head>
<title></title>
<script type="text/javascript">
var newElement = document.createElement("li");
newElement.innerHTML = "hello";
var boo = document.getElementById("moo");
boo.appendChild(newElement);
</script>
</head>
<body>
<ul id="moo">
<li>loo</li>
</ul>
</body>
</html>
does work:
<html>
<head>
<title></title>
<script type="text/javascript">
function hoo(){
var newElement = document.createElement("li");
newElement.innerHTML = "hello";
var boo = document.getElementById("moo");
boo.appendChild(newElement);
}
</script>
</head>
<body onclick="hoo()">
<ul id="moo">
<li>loo</li>
</ul>
</body>
</html>
What am i doing wrong that doesn't let it execute on its own? Thanks in advance.