0

Hello,
I'm a neophyte to Javascript, but I need it to dynamically add lines to a form.
I was able to cobble together this inelegant looking code below.
It adds the row when called, but there are more attributes needed for this to be correct for the application.
1) need to center the input in the TD
2)the input tags need type, size, maxsize, and value attributes added .
I have not been able to figure out the correct way to do this.

Also, is there a way for the value attribute to work with a PHP echo, or is there a Javascript method to display associative array values?

function addPart(id)
	{
		var tbody = document.getElementById
		(id).getElementsByTagName("TBODY")[0];
		var row = document.createElement("tr")
		var td1 = document.createElement("td")
		var td2 = document.createElement("td")
		var td3 = document.createElement("td")
		var td4 = document.createElement("td")
		var td5 = document.createElement("td")
		var td6 = document.createElement("td")
		td1.appendChild(document.createElement("input"))
		td2.appendChild(document.createElement("input"))
		td3.appendChild(document.createElement("input"))
		td4.appendChild(document.createElement("input"))
		td5.appendChild(document.createElement("input"))
		td6.appendChild(document.createElement("input"))
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		tbody.appendChild(row);
		
	}

BTW is there an good online tutorial and/or reference for javascript or book(s) that you could recommend?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by JRM
0

I think you want setAttribute for the most part. Other attributes like class and style are set directly...

td1.setAttribute("align","center");
td1.className="myclass";
td1.style.backgroundColor="red";
0

I think you want setAttribute for the most part. Other attributes like class and style are set directly...

td1.setAttribute("align","center");
td1.className="myclass";
td1.style.backgroundColor="red";

Thanks,
After hours of research (why is this stuff so hard to find?) in the mean time,
I have discovered that in a static layout like this one, alll of the attributes can be set directly.

var prtnum = document.createElement("input")
		prtnum.size =20
		prtnum.type ="text"
		prtnum.value = "" 
//can also use PHP here . partnum.value ="<?PHP echo $var;?>" just like in the html.

Now i'm tryimg to pass a PHP array to a JS array. No joy...but that's a new topic.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.