5

Sorry for the noob question: I want to create a table with input fields where the can add new ones, if they are needed. But I can'T figure out how add another input field inside a cell where another input field already exists.

My code is:

var par=obj.parentNode;
while(par.nodeName.toLowerCase()!='tr')
{ par=par.parentNode; }
// this gives me the index of the row.. works fine.

cell1=document.getElementById('avz_tabelle').rows[par.rowIndex].cells;
// this puts the content of the cells into the array "cell1"

var feld = cell1[1].createElement("input"); 
feld.setAttribute("name","avz_keywords" + avz_array + "[]"); 
feld.setAttribute("onblur","");
feld.setAttribute("type","text"); 
feld.setAttribute("size","30"); 
// Now I create a input element
// And now comes the problem:

cell1[1].appendChild(feld); // --> doesn't work

Has anyone a suggestion for me? I got the idea to work without a table, theoratically that would work with my way. But that wouldn't be satisfying :/

3
  • 2
    Is element.createElement() valid? I thought createElement() was only a method of document. Commented Dec 6, 2011 at 15:11
  • 1
    What is your html code? does the row of avz_tabelle contain even two cells? Why do you need to get that row index of an parentNode of obj?
    – Bergi
    Commented Dec 6, 2011 at 15:15
  • 1
    Where do you get which error? If you get none, please tell us what exactly works unexpected, if you get one post it.
    – Bergi
    Commented Dec 6, 2011 at 15:17

2 Answers 2

8

If you look in a debugging console, you should see something like

TypeError: Object #<HTMLDivElement> has no method 'createElement'

Instead of creating the element from cell1[1], use document.createElement()

 var feld = document.createElement("input");
2

You should use document.createElement("input"); instead of cell1[1].createElement("input");

As always (and as also stated in another answer here), the JavaScript console should be the first place to look for hints on what the problem is!

And wouldn't it actually be much easier if each table cell you want to add values to got a separate ID? Something containing perhaps the row index and the column index ... Then you could just select it directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.