Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    42
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Newbie - limit number of table rows

    Hi,

    Have a dynamic table in JS that adds rows. Want a max of 8 rows. How can that be done?

    [CODE]
    <script language="Javascript" type="text/javascript">
    function addRow()
    {
    var tbl = document.getElementById('ReqDtTbl');
    var lastRow = tbl.rows.length;
    var iteration = lastRow;
    var row = tbl.insertRow(lastRow);
    var cellLeft = row.insertCell(0);
    var textNode = document.createElement('input');
    textNode.size = 7;
    textNode.name = 'startdate' + iteration;
    cellLeft.appendChild(textNode);
    var cellRight = row.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'enddate' + iteration;
    el.id = 'enddate' + iteration;
    el.size = 7;
    cellRight.appendChild(el);
    // the last cell!
    var cellRightSel = row.insertCell(2);
    var sel = document.createElement('select');
    sel.name = 'TypeHrs' + iteration;
    sel.options[0] = new Option('-Select-', '""');
    sel.options[1] = new Option('Comp Time', 'Comp Time');
    sel.options[2] = new Option('Credit Hrs', 'Credit Hrs');
    sel.options[3] = new Option('Overtime', 'Overtime');
    sel.options[4] = new Option('Rel Comp', 'RelComp');
    cellRightSel.appendChild(sel);
    var cellRight = row.insertCell(3);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'No. of Hours' + iteration;
    el.id = 'No. of Hours' + iteration;
    el.size = 7;
    cellRight.appendChild(el);
    }
    [CODE]

    John

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,516
    Thanks
    3
    Thanked 505 Times in 492 Posts
    [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

    <head>
    <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/

    function addRow(id,nu)
    {
    var tbl = document.getElementById(id);
    var lastRow = tbl.rows.length;
    var iteration = lastRow;
    if (lastRow>nu){
    return;
    }
    var row = tbl.insertRow(lastRow);
    var cellLeft = row.insertCell(0);
    var textNode = document.createElement('input');
    textNode.size = 7;
    textNode.name = 'startdate' + iteration;
    cellLeft.appendChild(textNode);
    var cellRight = row.insertCell(1);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'enddate' + iteration;
    el.id = 'enddate' + iteration;
    el.size = 7;
    cellRight.appendChild(el);
    // the last cell!
    var cellRightSel = row.insertCell(2);
    var sel = document.createElement('select');
    sel.name = 'TypeHrs' + iteration;
    sel.options[0] = new Option('-Select-', '""');
    sel.options[1] = new Option('Comp Time', 'Comp Time');
    sel.options[2] = new Option('Credit Hrs', 'Credit Hrs');
    sel.options[3] = new Option('Overtime', 'Overtime');
    sel.options[4] = new Option('Rel Comp', 'RelComp');
    cellRightSel.appendChild(sel);
    var cellRight = row.insertCell(3);
    var el = document.createElement('input');
    el.type = 'text';
    el.name = 'No. of Hours' + iteration;
    el.id = 'No. of Hours' + iteration;
    el.size = 7;
    cellRight.appendChild(el);
    }

    /*]]>*/
    </script></head>

    <body>
    <table>
    <tbody id="ReqDtTbl" ></tbody>
    </table>

    <input type="button" name="" value="TEST" onclick="addRow('ReqDtTbl',7)"/></body>

    </html>[CODE]
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    42
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Vic,

    Thank you very much. Works perfectly.

    John


  •  

    Posting Permissions

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