Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8127195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:26:26+00:00 2026-06-06T07:26:26+00:00

I have the following code: <html> <body> <div style=background-color: lightblue; onClick=alert(myArray[0][1])> this is a

  • 0

I have the following code:

<html>
<body>
<div style="background-color: lightblue;" onClick="alert(myArray[0][1])">
this is a div
</div>

<script type="text/javascript">
var myArray = new Array();
myArray[0][0] = 0;
myArray[0][1] = 00012;
myArray[0][2] = 00006;
myArray[1][0] = 1;
myArray[1][1] = 00004;
myArray[1][2] = 00001;
</script>
</body>
</html>

When I click on the div, nothing happens; there’s no alert. When I change the inside of alert to a string, such as ‘test’, however, then the alert box does come up.

What am I doing wrong? How can I get the value of an item in a multidimensional array?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:26:28+00:00Added an answer on June 6, 2026 at 7:26 am

    The first line of your code:

    var myArray = new Array();
    

    …will create a new, single dimensional array, myArray, that has no elements. Then when you say:

    myArray[0][0] = 0;          
    

    …you are trying to access a dimension that doesn’t exist yet. That is, myArray[0] is undefined because although myArray is an array it doesn’t have any elements yet – so myArray[0][0] is like saying undefined[0].

    That’s why you have to to assign myArray[0] to refer to a new array before you can access myArray[0][0]. The same thing applies to myArray[1], because JavaScript doesn’t have multi-dimensional arrays per se, it has arrays of arrays. So this is what you need (for a minimal change to your existing code):

    var myArray = [];
    myArray[0] = [];
    myArray[0][0] =  00012;
    myArray[0][1] = 00012;
    myArray[0][2] = 00006;
    myArray[1] = [];
    myArray[1][0] = 1;
    myArray[1][1] = 00004;
    myArray[1][2] = 00001;
    

    Note that [] is equivalent to new Array().

    An easier to read and type option is to create the sub-arrays via array literal syntax:

    var myArray = [];
    myArray[0] = [00012, 00012, 00006];
    myArray[1] = [1, 00004, 00001];
    

    Or, easiest of all (especially if these are hard-coded values) is creating the whole thing in one statement via a nested array literal (white-space is ignored):

    var myArray = [
                    [00012, 00012, 00006],
                    [1, 00004, 00001]
                  ];
    

    (Note also that those leading zeros will disappear for numeric data: use strings ("00012" instead of 00012) if you want to retain the zeros.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: <HTML> <head> <style>div{border:dashed 1px silver}</style> </head> <BODY style=background: #fff;>
I have the following code <HTML> <HEAD> <style type=text/css> .setbgcolor{ background-color:blue; } </style> <script
I have the following html code. <html> <body> <div style=max-width:1600px; min-width:900px; > <table> </table>
The following Code <!DOCTYPE html> <html> <head> </head> <body> <div style=width:200px;height:200px;padding:0px;margin:0px;> <div style=background-color:red;position:relative;display:block;text-decoration:none;padding:0px;margin:0px;> <img
I have the following code <html> <head> <title>Test</title> <style type=text/css> <!-- body,td,th { color:
I have the following code : <html xmlns=http://www.w3.org/1999/xhtml> <body style=height: 1336px> <input type=hidden id=loadingtime/>
I have the following code that works fine in IE: <HTML> <BODY> <script language=JavaScript>
I have the following CSS: html, body { background-color:black; font-family:arial bold; overflow:auto; text-align: center;
I have the following code: <html> <head> <style type=text/css> #test img {vertical-align: middle;} div#test
I am a newbee with javascript and have the following code: <html> <body> <object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.