0

I have this code:

function getRowValue() {
       
    var table = document.getElementById('employee-table');
    var cells = table.getElementsByTagName('td');

    for (var i = 0; i < cells.length; i++) {
        // Take each cell
        var cell = cells[i];
        // do something on onclick event for cell
        cell.onclick = function () {
            // Get the row id where the cell exists
            var rowId = this.parentNode.rowIndex;
            var rowsNotSelected = table.getElementsByTagName('td');
            var rowSelected = table.getElementsByTagName('tr')[rowId];
          
            msg = rowSelected.cells[0].innerHTML;
        
            alert(msg);
            return msg;
        }   
    }
}

getRowValue();
alert(msg);

How can I access value of msg outside the function. I've tried with return on different places, push it to array, return false in loop ---- no result. Thanks!

  • There's no message at the time you're trying to refer it. `msg` will not be defined before you actually click a cell in the table. – Teemu Feb 24 '21 at 17:31
  • You can't get the value of the cell that was clicked before it has been clicked. – Quentin Feb 24 '21 at 17:34
  • Yes, but when I click on some row in table, it continue to show me no results. I try console.log when msg is not empty, and it doesn't matter how many times I,ve click, it still has no results. – Ivan_Mladenov Feb 24 '21 at 17:45

0 Answers0