Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array Alert

Im new to Javascript.

Im trying to code these four buttons. I'm currently on the second one. I've coded an array. But when I click on the button, it replaces the page. I want to display the array in an alert box.

<html> <head> <script type="text/javascript">  function SayHello() {     alert("Hello World!"); }  function DumpCustomers() {      var aCustomers=new Array();     aCustomers[0]="Frank Sinatra ";     aCustomers[1]="Bob Villa ";     aCustomers[2]="Kurt Cobain ";     aCustomers[3]="Tom Cruise ";     aCustomers[4]="Tim Robbins ";     aCustomers[5]="Santa Claus ";     aCustomers[6]="Easter Bunny ";     aCustomers[7]="Clark Kent ";     aCustomers[8]="Marry Poppins ";     aCustomers[9]="John Wayne ";      document.write(aCustomers[0]);     document.write(aCustomers[1]);     document.write(aCustomers[2]);     document.write(aCustomers[3]);     document.write(aCustomers[4]);     document.write(aCustomers[5]);     document.write(aCustomers[6]);     document.write(aCustomers[7]);     document.write(aCustomers[8]);     document.write(aCustomers[9]); }  function DisplayFishCounts() {  } function FindJonGalt() {  }  </script> </head>  <body> <form name="Main"> <input type="button" id=1 onclick="SayHello();" value="Say Hi"/> <input type="button" id=1 onclick="DumpCustomers();" value="Dump Customers"/> <input type="button" id=1 onclick="DisplayFishCounts();" value="Display Fish Counts"/> <input type="button" id=1 onclick="FindJonGalt();" value="Where is Jon Galt?"/>  </form> </body> </html> 
like image 519
chrisholdren Avatar asked Oct 27 '11 04:10

chrisholdren


People also ask

How do I get alerts in JavaScript?

The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.

Can we use alert in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.

What is JavaScript alert ()?

The alert() method displays an alert box with a message and an OK button. The alert() method is used when you want information to come through to the user.


1 Answers

If you want to see the array as an array, you can say

alert(JSON.stringify(aCustomers)); 

instead of all those document.writes.

http://jsfiddle.net/5b2eb/

However, if you want to display them cleanly, one per line, in your popup, do this:

alert(aCustomers.join("\n")); 

http://jsfiddle.net/5b2eb/1/

like image 78
Ray Toal Avatar answered Sep 19 '22 18:09

Ray Toal