0

Hi I have code as follows:

function addRowToTable(num){
if (hasLoaded) {
    var tbl = document.getElementById(TABLE_NAME);
    var nextRow = tbl.tBodies[0].rows.length;
    var iteration = nextRow + ROW_BASE;
    if (num == null) {
        num = nextRow;
    } else {
        iteration = num + ROW_BASE;
    }

    // add the row
    var row = tbl.tBodies[0].insertRow(num);

    // cell
    var cell0 = row.insertCell(0);
    var LNInpT = document.createElement('input');
    LNInpT.setAttribute('type', 'text');
            LNInpT.setAttribute('value', 'name');
            cell0.appendChild(LNInpT);

    // Pass in the elements you want to reference later
    // Store the myRow object in each row
    row.myRow = new myRowObject(textNode, txtInp, cbEl, raEl);
}}

Please help me to Add new Row before first row in table with editing this code. tanks

saber
  • 1
  • 1

3 Answers3

1

This should help you:

var myTable = document.getElementById('myTable');
var tbody = myTable.tbodies[0];
var tr = tbody.insertRow(-1); // puts it at the start

var td = document.createElement('td');
td.innerHTML = "Something";
tr.appendChild(td);
waldyr.ar
  • 14,424
  • 6
  • 33
  • 64
  • According to documentation of insertRow function. You should use 0 instead of -1 to add row as first row of the table. - https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement.insertRow – MicTech Nov 20 '13 at 20:53
0

I'd recommend you use jQuery which will make this much easier (with the prepend method). If you can't or don't want to, you should be able to use insertBefore.

You might want to look at this question on SO.

Community
  • 1
  • 1
Peter
  • 13,733
  • 11
  • 75
  • 122
0

Use jQuery's prepend() method for that.

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
rash111
  • 1,307
  • 4
  • 19
  • 35