0

I am trying to build a drop-down menu that will show a row to the side of it when you hover over one of the cells with the mouse. I'm really new with using the DOM, and javascript as well.

The problem I'm running into is that the array I have created doesn't seem to access what I thought it should. Is it just my syntax, or do I need to approach this differently? Here is my code:

window.onload = function () {
    var tableID = "strokerKitMenu";
    var table = document.getElementById(tableID);
    var tableRows = table.getElementsByTagName("tr");
    var tableCell = [];
    var counter = 0;
    for (i = 0; i < tableRows.length; i++) {
        tableCell[i] = new Array();
        tableCell[i].push(tableRows[i].getElementsByTagName("td"));
    }

    for (i = 0; i < tableCell.length; i++) {
        tableCell[0, i].style.display = "block"; //This doesn't compile
        alert(tableCell[0, i].Text); //This comes back "undefined"
        for (j = 1; j < tableCell[i].length; j++) //I haven't even tested this part yet
        {
            tableCell[i][j].onmouseover = function showCell() {
                tableCell[i][j + 1].style.display = "block";
            }
            tableCell[i][j].onmouseout = function hideCell() {
                this.style.display = "none";
            }
        } //end inner for loop
    } //end outer for loop
}
1
  • Please don't change your question to reflect the code given in an answer. It makes the answer obsolete and confusing. Commented Feb 23, 2012 at 18:18

2 Answers 2

1
tableCell[0, i].style.display="block";   //This doesn't compile

This syntax is invalid in JavaScript: you have specified two indices for tableCell but I guess you meant tableCell[0][i].style.display="block";

alert(tableCell[0, i].Text);             //This comes back "undefined"

Once you fix the first error, you will want to change this to tableCell[0][i].textContent.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried what you suggested, but I'm still getting the same errors. Other thoughts?
What is "same errors"? "Doesn't compile" and "undefined value"? Did you try any debugger? (Chrome Developer Tools or Firebug)
0

tableCell[i].push.apply(tableCell[i], tableRows[i].getElementsByTagName("td"));

You don't want to push the single array in. You want to push all the elements in. Otherwise you would have to access them as tableCell[i][0][j]

1 Comment

How come tableRows is a 2D array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.