<script type="text/javascript">
function addRowToTable()
{
var tbl = document.getElementById('myTable');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);
// right cell
var cellRight1 = row.insertCell(1);
var el = document.createElement('input');
el.type = 'text';
el.name = 'employeename' + iteration;
el.id = 'employeename' + iteration;
el.size = 10;
el.onkeypress = keyPressTest;
cellRight1.appendChild(el);
var cellRight2 = row.insertCell(2);
var e2 = document.createElement('input');
e2.type = 'text';
e2.name = 'gender' + iteration;
e2.id = 'gender' + iteration;
e2.size = 10;
e2.onkeypress = keyPressTest;
cellRight2.appendChild(e2);
var cellRight3 = row.insertCell(3);
var e3 = document.createElement('input');
e3.type = 'text';
e3.name = 'birthdate' + iteration;
e3.id = 'birthdate' + iteration;
e3.size = 10;
e3.onkeypress = keyPressTest;
cellRight3.appendChild(e3);
var cellRight4 = row.insertCell(4);
var e4 = document.createElement('input');
e4.type = 'text';
e4.name = 'zip' + iteration;
e4.id = 'zip' + iteration;
e4.size = 10;
e4.onkeypress = keyPressTest;
cellRight4.appendChild(e4);
var cellRight5 = row.insertCell(5);
var e5 = document.createElement('input');
e5.type = 'text';
e5.name = 'choice' + iteration;
e5.id = 'choice' + iteration;
e5.size = 10;
e5.onkeypress = keyPressTest;
cellRight5.appendChild(e5);
var cellRight6 = row.insertCell(6);
var e6 = document.createElement('input');
e6.type = 'text';
e6.name = 'members' + iteration;
e6.id = 'members' + iteration;
e6.size = 10;
e6.onkeypress = keyPressTest;
cellRight6.appendChild(e6);
var cellRight7 = row.insertCell(7);
var e7 = document.createElement('input');
e7.type = 'text';
e7.name = 'healthplan' + iteration;
e7.id = 'healthplan' + iteration;
e7.size = 10;
e7.onkeypress = keyPressTest;
cellRight7.appendChild(e7);
// select cell
}
function keyPressTest(e, obj)
{
var validateChkb = document.getElementById('chkValidateOnKeyPress');
if (validateChkb.checked) {
var displayObj = document.getElementById('spanOutput');
var key;
if(window.event) {
key = window.event.keyCode;
}
else if(e.which) {
key = e.which;
}
var objId;
if (obj != null) {
objId = obj.id;
} else {
objId = this.id;
}
displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
}
}
function removeRowFromTable()
{
var tbl = document.getElementById('myTable');
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);
}
function validateRow(frm)
{
var tbl = document.getElementById('myTable');
var lastRow = tbl.rows.length - 1;
var i;
for (i=1; i<=lastRow; i++) {
var employeename = document.getElementById('employeename' + i);
var gender = document.getElementById('gender' + i);
var birthdate = document.getElementById('birthdate' + i);
var zip = document.getElementById('zip' + i);
var choice = document.getElementById('choice' + i);
var members = document.getElementById('members' + i);
var healthplan = document.getElementById('healthplan' + i);
if ((employeename.value.length <= 0)) {
alert('employeename in row ' + i + ' is empty');
return false;
}
if ((gender.value.length <= 0)) {
alert('gender in row ' + i + ' is empty');
return false;
}
if ((birthdate.value.length <= 0)) {
alert('birthdate in row ' + i + ' is empty');
return false;
}
if ((zip.value.length <= 0)) {
alert('zipcode in row ' + i + ' is empty');
return false;
}
if ((choice.value.length <= 0)) {
alert('Coverage Choice in row ' + i + ' is empty');
return false;
}
if ((members.value.length <= 0)) {
alert('# of Members in row ' + i + ' is empty');
return false;
}
if ((healthplan.value.length <= 0)) {
alert('Curent Health Plan in row ' + i + ' is empty');
return false;
}
}
return true;
}
</script>
<form name="formx" action="" method="get">
<input type="button" value="Add" onclick="addRowToTable();" />
<input type="button" value="Remove" onclick="removeRowFromTable();" />
<input type="button" value="Submit" onclick="validateRow(this.form);" />
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#EAEAEA" >
<tr>
<td colspan="7" align="left" valign="top" bgcolor="#F5F8FA"><strong>Company Name: </strong></td>
</tr>
<tr>
<td colspan="4" align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<textarea name="cdetails" cols="28" rows="4" class="contact_form"></textarea>
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF" class="normal_text">(EE=Employee only,<br /> ES=Employee + Spouse, <br />EC=Employee + Child(ren),</td>
<td colspan="2" align="left" valign="top" bgcolor="#FFFFFF" class="normal_text"> </td>
</tr>
</table>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#EAEAEA" id="myTable">
<tr>
<td width="21%" height="30" align="left" valign="top" bgcolor="#F5F8FA"><strong>S.no:</span></strong></td>
<td width="21%" height="30" align="left" valign="top" bgcolor="#F5F8FA"><strong>Employee Name (First Last):</span></strong></td>
<td width="9%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong>Gender:</strong></td>
<td width="9%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong>Birthdate:</strong></td>
<td width="12%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong>Home Zip Code:</strong></td>
<td width="25%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong>Coverage Choice (EE, ES, EC, FA):</strong></td>
<td width="11%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong># of Members:</strong></td>
<td width="13%" height="30" align="left" valign="top" bgcolor="#F5F8FA" class="normal_text"><strong>Curent Health Plan</strong></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<span class="normal_text">
<label class="form_txt_gender">1</label>
</span></td>
<td bgcolor="#FFFFFF">
<span class="normal_text">
<input name="employeename1" id="employeename1" type="text" class="form_txt_gender" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="gender1" id="gender1" type="text" class="form_txt_gender" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="birthdate1" id="birthdate1" type="text" class="form_txt_gender" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="zip1" id="zip1" type="text" class="form_txt_gender" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="choice1" id="choice1" type="text" class="form_txt_1" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="members1" id="members1" type="text" class="form_txt_gender" size="30" />
</span></td>
<td align="left" valign="top" bgcolor="#FFFFFF"><span class="normal_text">
<input name="healthplan1" id="healthplan1" type="text" class="form_txt_1" size="30" />
</span></td>
</tr></table>
</form>
OUTPUT: