Creating dynamic tables using javascript, given rows and column in textboxes

后端 未结 3 1234
梦如初夏
梦如初夏 2021-01-07 00:22

I am back with a problem again (please cooperate with my silly questions, i am very new to JavaScript). I am trying for understanding the concept of dynamic tables in JavaS

3条回答
  •  生来不讨喜
    2021-01-07 00:44

    JS

    function createTable() {
        var a, b, tableElem, rowElem, colElem;
    
        a = document.getElementById('tb1').value;
        b = document.getElementById('tb2').value;
    
        if (a == "" || b == "") {
            alert("Please enter some numeric value");
        } else {
            tableElem = document.createElement('table');
    
            for (var i = 0; i < a; i++) {
                rowElem = document.createElement('tr');
    
                for (var j = 0; j < b; j++) {
                    colElem = document.createElement('td');
                    colElem.appendChild(document.createTextNode(j + 1)); //to print cell number
                    rowElem.appendChild(colElem);
                }
    
                tableElem.appendChild(rowElem);
            }
    
            document.body.appendChild(tableElem);
    
    
        }
    }
    

    JSFiddle
    http://jsfiddle.net/mprRb/1/

提交回复
热议问题