Manu Padmanabhan
Manu Padmanabhan

Reputation: 555

how to add a div for dynamic table Td (not Th)

 function table() {
            var body = document.body,
                     tbl = document.createElement('table'),
                     tableId = document.createAttribute('id');
            tableId.value = "table";
            tbl.setAttributeNode(tableId);
            tbl.className = 'table2';
            var id = 0;
            for (var i = 0; i < 30; i++) {
                var tr = tbl.insertRow();
                tr.setAttribute("data-id", i, 0);

                for (var j = 0; j < 4; j++) {

                    var td = tr.insertCell();
                    td.appendChild(document.createTextNode(""));

                    td.id = "td" + id ;
                    id++;
                }

            }
            
           
            $(".middletablediv").append(tbl);
           
        };
        table();

i have dynamic table which contain td and th i add div for table td but it also effect the th .i don't want that div in th i want div only in div

$('tr:first-child').children('td').replaceWith(function(i, html) {
  return '<th>' + html + '</th>';
});

this code is used for change first row td into th and

$('#table td').wrapInner('<div class="tddivcolor divtime" />');

this for adding class to td

Upvotes: 0

Views: 77

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

The only reason I could see is the order of your script execution.

If you are running the wrapping code first then the td->td conversion code then the divs will get added to the first row also.

So one solution is to fix the order and move the th conversion code first.

If that is not possible then you can exclude the first row from wrapping

$('#table tr:not(:first-child) td').wrapInner('<div class="tddivcolor divtime" />');

Upvotes: 1

Manjuboyz
Manjuboyz

Reputation: 7066

If you know the specific, try to use append if it works for you.

<table><tr><td>...

this not be the exact answer you need, but might lead to proper direction.

like where you want to put div, then try this, i used this to my selector class to create 'div'

$("#SelectorID").append("<div class= EmpList id=" + empID + ">" + selectedList[empID] + " <span class='close'>&times;</Span> </div>");

Upvotes: 0

Related Questions