Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create checkbox column in the table?

I am dynamically creating a table using JavaScript code below:

function CreatTable(data) {

    var tablearea;
    var table;
    var thead;
    var tr;
    var th;

    tablearea = document.getElementById('ShowDataID');
    table = document.createElement('table');
    thead = document.createElement('thead');
    tr = document.createElement('tr');

    for (var i = 0; i < data.length; i++) {
        var headerTxt = document.createTextNode(data[i]);
        th = document.createElement('th');
        th.appendChild(headerTxt);
        tr.appendChild(th);
        thead.appendChild(tr);
    }

    table.appendChild(thead);

    for (var i = 1; i < 4; i++) {
        tr = document.createElement('tr');
        tr.appendChild(document.createElement('td'));
        tr.appendChild(document.createElement('td'));
        tr.appendChild(document.createElement('td'));
        tr.cells[0].appendChild(document.createTextNode('John'));
        tr.cells[1].appendChild(document.createTextNode('McDowell'));
        tr.cells[2].appendChild(document.createTextNode('[email protected]'));

        table.appendChild(tr);
    }
    tablearea.appendChild(table);
}
</script>

When I create table I also need to create checkbox column in the table above.

Any idea how I can implement it using JavaScript? Or related link?

like image 329
Michael Avatar asked Jan 17 '26 02:01

Michael


1 Answers

I'm not sure if this is what you're asking:

var checkbox = document.createElement("INPUT");
checkbox.type = "checkbox";

and then you'd append checkbox to each row in order to form a new column.

See this fiddle: http://jsfiddle.net/qeeu18g1/3/ I added comments where I added things.

(is this a duplicate of this question?)

like image 55
dosaki Avatar answered Jan 19 '26 15:01

dosaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!