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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to update all input tags dynamically

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
US
Hi all experts,

I am trying to rename input tags for the purposes of reusing a particular table to create new instances on the same page.

for some reason the following code(alert(table_content.innerHTML) shows no changes, but when the alert in the for loop is uncommented it appears to be changing the value:

function addLineSubSection(divName,table_content){
var randomnumber=Math.floor(Math.random()*9999)
var output = document.getElementById(divName).innerHTML ;

for(i = 0; i < table_content.getElementsByTagName("input").length; i++) {
table_content.getElementsByTagName("input").setAttribute("name",table_content.getElementsByTagName("input").getAttribute("name") + "_" + randomnumber);
//alert(table_content.getElementsByTagName("input").getAttribute("name"));
}
// //text += spanTag.childNodes.nodeValue;
alert(table_content.innerHTML);
output += table_content.innerHTML;
document.getElementById(divName).innerHTML = output;
}

thanks in advance
 
Did you just try accessing the name attribute directly instead of using the DOM method? Support for DOM methods is kind of sketchy across different browsers, try something like this:
Code:
alert(table_content.getElementsByTagName("input")[i].[!]name[/!]);
Just on a side note, you could cut down your code immensely by storing table_content.getElementsByTagName("input") in an array variable. Additionally, referencing table_content.getElementsByTagName("input").[!]length[/!] is a timely process when ran in a loop like you're doing. If you set table_content.getElementsByTagName("input") to an array variable above the loop, you can then put table_content.getElementsByTagName("input")[!].length[/!] into a static variable and it should speed up the process when used in the comparison element of your for loop. Of course, if you don't have that many input elements on your page then it probably won't make that much of a difference, but it's still good practice.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
that works as well, but when I try

alert(table_content.innerHTML);

after the changes, I view the innerHTML and none of the changes are there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top