Beginner javascript arrays

Jacob Bradley

I am new to JavaScript and I am trying to do something very simple. I wan to appear array[1] text in firstDiv's innerHTML when I click button.

I have followed all instructions but still it's not working.

<!doctype html>
<html>
<head>
    <title>Learning Javascript</title>

    <meta charset="utf-8" />
    <meta htttp-equiv="content-type" contents="text/html; charset-utf8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />  

</head>

<body>

    <button id="stylesChanger">Change the text !</button>

    <div id="firstDiv">This sis some text</div>

    <script type="text/javascript">

        var myArray=new Array[];

        myArray[0]="pizza";
        myArray[1]="chocolate";

        document.getElementById("stylesChanger").onclick=function(){

            document.getElementById("firstDiv").innerHTML=myArray[1];
        }               

        </script

</body>
</html>
maxspan

This code will make sure you get the first element of myArray on button click. And sets the div text as myArray first element.

Working Sample: JSFIDDLE

var myArray = new Array();
myArray[0] = 'pizza';
myArray[1] = 'chocolate';

var btn = document.getElementById('stylesChanger');
btn.addEventListener('click', getArrayFirstElement, false);

function getArrayFirstElement(){
 document.getElementById("firstDiv").innerHTML = myArray[0];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related