0

I'm trying to understand how constructors works. Here is the first part of my table constructor:

function build(rowsNum,cellsNum,id){

    this.id=document.getElementById(id);

    this.addRow=function(){
        var row=id.insertRow();

        for(var j=0;j<cellsNum.length;j++){
            var cell=row.insertCell();
        }
    };
}
var test=new build(1,2,firsttbody);
test.addRow();

The problem is that the method test.addRow(); don't add cells. I have already checked that my browser supports insertCell(), but I can't understand, what is wrong. In console there are no errors.

Aramil Rey
  • 3,387
  • 1
  • 19
  • 30
Vasya
  • 1
  • If its not a variable change to `build(1,2,"firsttbody");` as `getElementById()` wants a string. `cellsNum` is a Number, it has no `length` property. - Your F12 Error Console will help with this. – Alex K. May 06 '16 at 16:37
  • `insertRow()` takes an index. so does `insertCell()`. You have to tell it what position to do the insert. – devlin carnate May 06 '16 at 16:38
  • Possible duplicate of [How to insert row in HTML table body in javascript?](http://stackoverflow.com/questions/18333427/how-to-insert-row-in-html-table-body-in-javascript) – devlin carnate May 06 '16 at 16:40
  • 1
    @AlexK. error was that cellsNum has no length, I didn't notice that because of fatigue. And with build(1,2,"firsttbody"); I have error: Uncaught TypeError: id.insertRow is not a function – Vasya May 06 '16 at 16:51
  • `this.id` is the element, `id` is its name so `this.id.insertRow(XXX);` where `XXX` is an index you need to provide as mentioned above. – Alex K. May 06 '16 at 16:55
  • @AlexK. thank you, it works now! – Vasya May 06 '16 at 17:09

0 Answers0