Click table row and get value of all cells

后端 未结 5 908
温柔的废话
温柔的废话 2020-12-31 16:05

I don\'t know JQuery, so I\'m hoping there is a way to do this in pure Javascript.

I need to click on a table row and get the value of each cell in that row. Here i

5条回答
  •  一整个雨季
    2020-12-31 16:47

    Check this fiddle link

    HTML:

    OCB Area Name Cell # Nickname
    275 Layton Installation Benjamin Lloyd (801) 123-456 Ben

    JAVASCRIPT:

    init();
    function init(){
    
        addRowHandlers('rowCtr');
    
    }
    
    function addRowHandlers(tableId) {
        if(document.getElementById(tableId)!=null){
            var table = document.getElementById(tableId);
            var rows = table.getElementsByTagName('tr');
            var ocb = '';
            var area = '';
            var name = '';
            var cell = '';
            var nick = '';
            for ( var i = 1; i < rows.length; i++) {
    
                rows[i].i = i;
                rows[i].onclick = function() {
    
                    ocb = table.rows[this.i].cells[0].innerHTML;                
                    area = table.rows[this.i].cells[1].innerHTML;
                    name = table.rows[this.i].cells[2].innerHTML;
                    cell = table.rows[this.i].cells[3].innerHTML;
                    nick = table.rows[this.i].cells[4].innerHTML;
                    alert('ocb: '+ocb+' area: '+area+' name: '+name+' cell: '+cell+' nick: '+nick);
                };
            }
        }
    }
    

提交回复
热议问题