Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add properties to dynamically added <td> 1

Status
Not open for further replies.

mwa

Programmer
Jul 12, 2002
507
US
thread216-553813 explains how to dynamically add table rows. This works great, but what if you need to add properties such as colspan, rowspan, class, width, etc.

How is this done?

mwa
<><
 
Here is a snippet from some code I wrote before.

Code:
// create an element whose tag name is TD and fill in attributes
mycurrent_cell=document.createElement("TD");
mycurrent_cell.colSpan = 6;
mycurrent_cell.className = "h1";
mycurrent_cell.width = 100;
 
I can't seem to get this working. Here is my code:

Code:
<style>
TD.header3
{
	font-weight: bolder;
	font-size: 12pt;
	color: black;
	background-color: lightgoldenrodyellow;
}
</style>
<script>
//create a variable for adding rows
var intNumber = 1
function addRow(inTable){
	//increment row number by 1
	intNumber += 1
	document.form1.number.value = intNumber
	tableNode = document.getElementById(inTable)
	//create new table row
	newRow = document.createElement("tr")
	//create new table data
	newCell = document.createElement("td")
	newCell.classname = "TD.header3";
	newCell.innerHTML = "Reason " + intNumber + ":"
	newRow.appendChild(newCell)
	newCell = document.createElement("td")
	newCell.innerHTML = "<input type=text id=txtTravelReason" + intNumber + " name=txtTravelReason" + intNumber + " style='width:100%;'>"
	newRow.appendChild(newCell)
	//add new row to table
	tableNode.firstChild.appendChild(newRow)	 
}
</script>
<form id=form1 name=form1>
	<table border=1 width=100% ID="travel" cols=4>
					<tr>
						<td class=header3 width=25%>Reason 1:</td>
						<td colspan=3><input type=text id="txtTravelReason2" name=txtTravelReason2 style="width:100%;"></td>
					</tr>
		
	</table>
<input type=hidden value=1 id=number><br>
<input type=button onClick="addRow('travel')" value="Add" ID="Button2" NAME="Button2">
</form>

mwa
<><
 
Change the following line

newCell.classname = "TD.header3";

to
newCell.className = "header3";

className is case sensitive and the TD. refers to the tag.
 
Rock On... That took care of it... I actually started with "header3" but I added the TD trying to get it to work. It was the case sensitivity that got me...

Thanks for your help...

mwa
<><
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top