I have a JSP page where I dynamically add rows to a table using a different Javascript function than in my previous query. While I can add elements to the table columns, I'm unable to apply a style class that is defined in a CSS file.
Here is my Javascript function:
function addRow(tableID) {
try{
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount-1);
var i=0;
var newCell = row.insertCell(i);
newCell.innerHTML ='<h4>Type </h4>';
i++;
newCell = row.insertCell(i);
newCell.innerHTML ='<input type="text" name="type7" id="type8" size="30"/>';
i++;
newCell = row.insertCell(i);
newCell.innerHTML ='';
i++;
newCell = row.insertCell(i);
newCell.innerHTML ='<h4>Description </h4>';
i++;
newCell = row.insertCell(i);
newCell.innerHTML ='<textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea>';
} catch(e) {
alert(e);
}
}
Here is my HTML snippet:
<table id="table1" width="792" border="0">
<tr class="rowdiv">
<td class="formlabel"><h4>Type </h4></td>
<td class="formfield"><input type="text" name="type7" id="type8" size="30"/></td>
<td class="formgap"></td>
<td class="formlabel"><h4>Description </h4></td>
<td class="formfield"><textarea name="textarea2" id="textarea2" cols="28" rows="2"></textarea></td>
</tr>
<tr class="rowdiv">
<td width="170" class="formlabel"> </td>
<td class="formfield"> </td>
<td class="formgap"></td>
<td class="formlabel"> </td>
<td class="formfield"><h6 onclick="addRow('table1')">Add policy</h6></td>
</tr>
</table>
I want to apply the same styles (classes formlabel, formfield, and formgap) to newly created rows as well.
I tried searching on Google but found codes that extract style attributes one by one and copy them to a new row, whereas I want to include the class names directly.
Here is my CSS section:
.formlabel{
text-align: right;
font: Verdana, Geneva, sans-serif;
font-weight: lighter;
margin: 0px;
padding: 0px;
text-transform: capitalize;
color: #000000;
}
/* More CSS rules here */
.formfield input {text-transform: capitalize; font:Verdana, Geneva, sans-serif;}
.formlabel h5{margin: 0px; padding: 0px; font-weight: lighter;}
.formfield h6{margin: 0px; padding: 0px; font-weight: lighter;}