javascript .push() doesn't seem to work
Posted
by Ankur
on Stack Overflow
See other posts from Stack Overflow
or by Ankur
Published on 2010-05-09T02:39:47Z
Indexed on
2010/05/09
2:48 UTC
Read the original article
Hit count: 298
JavaScript
|arrays
I am trying to use .push to put items into a javascript array. I have created a simplified piece of code. When I click the button with id clickButton, I am expecting to get a series of alerts with numbers, but I get nothing.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sempedia | Making computers think about data the way we do</title>
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script>
$(document).ready( function() {
var myArray = new Array(); //declare the array
for(var i=0;i<=10;i++){ // create a loop
myArray.push(i); // add an item to array
}
$("#clickButton").live('click',function() { //register the button being clicked
for(var j=0;j<=10;j++){ //for loop
alert(myArray[j]); //alert one item in the array
}
});
});
</script>
</head>
<body>
<input type="button" value="click me" id="clickButton" />
</body>
</html>
© Stack Overflow or respective owner