I've encountered a weird problem with Javascript. What I was given is a string of certain format that I will try to create a table out of.
The table will only have a cell per row and the format of the string is:
Content needs to be displayed for each cell (row)
@
parameter that will be passed to the onmouseover
event handler which gets called when the user moves their mouse over the displayed text.
The JS code:
// the string of format
var stringProof = document.getElementById("stringProof").value;
var arrayOfProof = stringProof.split("#");
// find the table
var table = document.getElementById("proofTable");
// remove what's within
table.innerHTML = "";
for(var i = currentIndex*4;i < end;i++)
{
// iterative create the text, span, cell and row
var currentStepProof = arrayOfProof[i];
var elementsInCurrentStepProof = currentStepProof.split("@");
var tr = document.createElement("tr");
var td = document.createElement("td");
var span = document.createElement("span");
span.onmouseover = function() {alert(elementsInCurrentStepProof[1]);};
var text = document.createTextNode(elementsInCurrentStepProof[0]);
span.appendChild(text);
td.appendChild(span);
tr.appendChild(td);
table.appendChild(tr);
}
The problem is it does not matter which rows the onmouseover
function is triggered, it will only alert the parameter of the last row which means the parameter of last row's onmouseover
function overwrites what was passed to the previous row's onmouseover
functions.
Any thoughts? Thanks a lot!!~~