SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    create element problem

    hi!
    why the cloneNode works and createElement not???
    hth
    Code:
    function addColumnsAndRows(iTableID, iRowsAdd, iColsAdd)
    {
      var objTable = document.getElementById(iTableID);
      if(objTable) {
        var i, j, ndNewRow, ndNewCol, iColNum, iRowNum;
        var ndTableRows = objTable.tBodies[0].rows;
        iRowNum = ndTableRows.length;
        for (i = 0; i < iRowsAdd; i++) {
          ndNewRow = ndTableRows[0].createElement("<input type='text' id='test'>");
          clearClonedInputValues(ndNewRow);
          objTable.tBodies[0].appendChild(ndNewRow);
        } 
        ++iRowNum;
        iColNum = ndTableRows[0].cells.length;
        for (j = 0; j < iColsAdd; j++) {
          for (i = 0; i < objTable.tBodies[0].rows.length; i++) {
            ndNewCol = ndTableRows[0].cells[0].cloneNode(true);
            clearClonedInputValues(ndNewCol);
            ndTableRows[i].appendChild(ndNewCol);
          }
        }
        ++iColNum;
      }
      initInputs();
    }
    any suggestions?
    thanx in advance!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about something like this:

    var input = document.createElement("<input type='text' id='test'>"); //I didn't know you could do that
    var td=createElement("td");
    td.appendChild(input);
    ndTableRows[0].appendChild(td);

    **certified 100% untested**

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *snicker*

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    solved..
    hth
    Code:
    function addColumnsAndRows(iTableID, iRowsAdd, iColsAdd)
    {
      var objTable = document.getElementById(iTableID);
      if(objTable) {
        var i, j, a, ndNewRow, ndNewCol, iColNum, iRowNum, iCol, iRow, inp, tr, td;
        var ndTableRows = objTable.tBodies[0].rows;
        iRow = ndTableRows.length;
        iCol = ndTableRows[0].cells.length;
        iRowNum = ndTableRows.length;
        for (i = 0; i < iRowsAdd; i++) {
        tr = document.createElement("tr");
        for (a = 0; a < iCol; a++) {  // solved
        inp = document.createElement("input");
        td = document.createElement("td");
        td.appendChild(inp);
        tr.appendChild(td);
    }
          ndNewRow = ndTableRows[0].appendChild(tr);
          objTable.tBodies[0].appendChild(ndNewRow);
    }
        ++iRowNum;
        iColNum = ndTableRows[0].cells.length;
        for (j = 0; j < iColsAdd; j++) {
          for (i = 0; i < objTable.tBodies[0].rows.length; i++) {
            ndNewCol = ndTableRows[0].cells[0].cloneNode(true);
            clearClonedInputValues(ndNewCol);
            ndTableRows[i].appendChild(ndNewCol);
          }
        }
        ++iColNum;
      }
      initInputs();
    }

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi!
    i need a desperate help here...
    im stucked all day about this code....
    i cant match the generated id for a generated input with a function getVal...
    hth
    Code:
    function addColumnsAndRows(iTableID, iRowsAdd, iColsAdd)
    {
      var objTable = document.getElementById(iTableID);
      if(objTable) {
        var i, j, a, ndNewRow, ndNewCol, iColNum, iRowNum, iCol, iRow, inp, tr, td, image;
        var ndTableRows = objTable.tBodies[0].rows;
        iRow = ndTableRows.length;
        iCol = ndTableRows[0].cells.length;
        iRowNum = ndTableRows.length;
        for (i = 0; i < iRowsAdd; i++) {
        tr = document.createElement("tr");
        for (a = 0; a < iCol; a++) {
        inp = document.createElement("input");
        inp.setAttribute('class', 'cloneInp');
        inp.setAttribute('id', iRowNum + a); //this id
        image = document.createElement("img");
        image.setAttribute('src', 'img/com.gif');
        image.setAttribute('class', 'cloneImg');
        image.setAttribute('onClick', 'showPopup("nameFieldPopup", event);getVal(iRowNum + a);'); // with this getVal property
        td = document.createElement("td");
        td.setAttribute('class', 'cloneTd');
        td.appendChild(inp);
        td.appendChild(image);
        tr.appendChild(td);
        document.getElementById("ta").innerHTML = a+""+iRowNum;
         }
        ++iRowNum;
          ndNewRow = ndTableRows[0].appendChild(tr);
          objTable.tBodies[0].appendChild(ndNewRow);
        }
        iColNum = ndTableRows[0].cells.length;
        for (j = 0; j < iColsAdd; j++) {
          for (i = 0; i < ndTableRows.length; i++) {
        inp = document.createElement("input");
        inp.setAttribute('class', 'cloneInp');
        inp.setAttribute('id', iColNum + i); //same here
        image = document.createElement("img");
        image.setAttribute('src', 'img/com.gif');
        image.setAttribute('class', 'cloneImg');
        image.setAttribute('onClick', 'showPopup("nameFieldPopup", event);getVal(iColNum + i);'); // and here
        td = document.createElement("td");
        td.setAttribute('class', 'cloneTd');
        td.appendChild(inp);
        td.appendChild(image);
        ndTableRows[i].appendChild(td);
    document.getElementById("ta").innerHTML = inp.id;
          }
        ++iColNum;
        }
      }
      initInputs();
    }
    function getVal(num) {
    if(document.getElementById(num).value == "") {
    document.getElementById("valz").innerHTML = "This field is empty!";
    } else {
    document.getElementById("valz").innerHTML = document.getElementById(num).value;
    }
    }
    i commented the lines into the code...
    any help appreciated!
    thanx in advance!

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i cant get work getVal function from the addcolumnandrows function..
    hth
    Code:
    function addColumnsAndRows(iTableID, iRowsAdd, iColsAdd)
    {
      var objTable = document.getElementById(iTableID);
      if(objTable) {
        var i, j, a, ndNewRow, ndNewCol, iColNum, iRowNum, iCol, iRow, inp, tr, td, image;
        var ndTableRows = objTable.tBodies[0].rows;
        iRow = ndTableRows.length;
        iCol = ndTableRows[0].cells.length;
        iRowNum = ndTableRows.length;
        for (i = 0; i < iRowsAdd; i++) {
        tr = document.createElement("tr");
        for (a = 0; a < iCol; a++) {
        inp = document.createElement("input");
        inp.setAttribute('class', 'cloneInp');
        inp.setAttribute('id', iTableID + a + '' + iRowNum);
        image = document.createElement("img");
        image.setAttribute('src', 'img/com.gif');
        image.setAttribute('class', 'cloneImg');
        image.setAttribute('onClick', 'showPopup("nameFieldPopup", event);getVal(inp.id)'); // here i call the function
        td = document.createElement("td");
        td.setAttribute('class', 'cloneTd');
        td.appendChild(inp);
        td.appendChild(image);
        tr.appendChild(td);
         }
        ++iRowNum;
          ndNewRow = ndTableRows[0].appendChild(tr);
          objTable.tBodies[0].appendChild(ndNewRow);
        }
        iColNum = ndTableRows[0].cells.length;
        for (j = 0; j < iColsAdd; j++) {
          for (i = 0; i < ndTableRows.length; i++) {
        inp = document.createElement("input");
        inp.setAttribute('class', 'cloneInp');
        inp.setAttribute('id', iTableID + i + '' + iColNum);
        image = document.createElement("img");
        image.setAttribute('src', 'img/com.gif');
        image.setAttribute('class', 'cloneImg');
        image.setAttribute('onClick', 'showPopup("nameFieldPopup", event);getVal(inp.id)'); // here i call the function
        td = document.createElement("td");
        td.setAttribute('class', 'cloneTd');
        td.appendChild(inp);
        td.appendChild(image);
        ndTableRows[i].appendChild(td);
          }
        ++iColNum;
        }
      }
      initInputs();
    }
    function getVal(numt) {
    if(document.getElementById(numt).value == "") {
    document.getElementById("valz").innerHTML = "This field is empty!";
    } else {
    document.getElementById("valz").innerHTML = document.getElementById(numt).value;
    }
    }
    i get the inp.id correctly when i test it with innerHTMl into some other field...
    the lines in question r commented!
    any help will be really much appreciated!!!
    thanx in advance!

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help!
    the getVal dont give the id from inp variable in setAttribute...
    Last edited by reminder; Dec 4, 2004 at 03:54.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    problem solved...
    thanx anyway!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •