[RESOLVED]Demo error

demo error – null or undefined

<script type="text/javascript">
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
var adds = ["Lemon","Tangerine","Kiwi"];
function myFunction() {
    for (i=0; i<2;i++)
{

    fruits.unshift(adds[i]);
}
    document.getElementById("demo").innerHTML = fruits;
}
</script>    
<title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
   <p>The unshift method adds new elements to the beginning of an array.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo" >Hello</p>
 
    </div>
    </form>
</body>
</html>

Your script executes as soon as the browser receives it, so it is accessing the "demo" node that doesn’t yet exist.  Move your script block to the bottom of the page, below the things it accesses.

Leave a Reply