Tuesday, October 18, 2016

Add a new Row on OnClick Using JavaScript

///////////////////////////////////////////////////////////////////////////////////////////////////
//// index.php
///////////////////////////////////////////////////////////////////////////////////////////////////
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>


</head>

<script language="javascript" type="text/javascript">

    function addField() {
    var tbody = document.getElementById("tblBody");
    var ctr = tbody.getElementsByTagName("input").length + 1;
    var input;
 
    if ( ctr > 15 ) {
          alert ("If you want to tell the whole world, dont do it all at once please");
    }else{
 
        if (document.all){ //input.name doesn't work in IE
            input = document.createElement('<input name="field_'+ctr+'">');
        }else{
            input = document.createElement('input');
            input.name = "field_"+ctr;
        }

        input.id = input.name;
        input.type = "text";
        input.value = "";
        input.className = "textfield";
        var cell = document.createElement('td');
        cell.style.height = '30px';
        cell.appendChild(document.createTextNode(ctr+". "));
        cell.appendChild(input);
        var row = document.createElement('tr');
        row.appendChild(cell);
        tbody.appendChild(row);
 
        window.document.the_form.count.value = ctr;
 
    }
}
</script>



<body>




    <form name="the_form" id="the_form" method="post" action="">
   
          <table width="100%"  border="0" cellspacing="0" cellpadding="0">
          <tbody id="tblBody">
            <tr>
              <td height="30">
                1. <input name="field_1" type="text" class="textfield" id="field_1" />
              </td>
            </tr>
            <tr>
              <td height="30">
                2. <input name="field_2" type="text" class="textfield" id="field_2" />
              </td>
            </tr>
            <tr>
              <td height="30">
                3. <input name="field_3" type="text" class="textfield" id="field_3" />
              </td>
            </tr>
            <tr>
              <td height="30">
                4. <input name="field_4" type="text" class="textfield" id="field_4" />
              </td>
            </tr>
           
        <tbody
        </table>
          <input name="count" type="hidden" id="count" value="4"/>      
          <input name="add" type="button" class="button" id="add" value="Add Another" onClick="addField();"/>        

    </form>
   

</body>
</html>
///////////////////////////////////////////////////////////////////////////////////////////////////
//// End index.php
///////////////////////////////////////////////////////////////////////////////////////////////////


No comments:

Post a Comment