•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 391,993 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,276 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1990 | Replies: 17 | Solved
![]() |
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
Hi,
I have a table in a form like this
I also have a button to add a new row, by cloning the 'dolly' tr and appending to 'target'
May I ask what is the javascript or DOM code to reach the <input> element so that I can change its name attribute? Something like this:
I have a table in a form like this
<form> <table> <tbody id='target'> <tr id="dolly"><td><input name="hobby[0]" /></td></tr> </tbody> </table> </form>
I also have a button to add a new row, by cloning the 'dolly' tr and appending to 'target'
<input type="button" onclick="AddRow('dolly', 'target') />May I ask what is the javascript or DOM code to reach the <input> element so that I can change its name attribute? Something like this:
newRow.td.input.name = "hobby[1]";
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation:
Rep Power: 3
Solved Threads: 16
This may help u:
javascript Syntax (Toggle Plain Text)
var inputs = document.getElementsByTagName("input"); var i; for(i=0; i < inputs.length; i++) inputs[i].name="my_name";
When you think you have done a lot, then be ready for YOUR downfall.
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation:
Rep Power: 3
Solved Threads: 16
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
•
•
•
•
there are many DOM function to add/del new rows of to create objects. Better google!!
Hi LuckyChap,
I tried googling. and I think I still have problem.
The problem occurs in IE7.
After changing the name using
clonedfield = document.getElementsByName('hobby[0]')[1];
clonedfield.name = 'hobby[1]';I cannot reference it in another block of code with
// error: clonedfield undefined in IE7. Works in FF2.
clonedfield = document.getElementsByName('hobby[1]')[0];Why is this so?
ADD:
As a matter of fact, IE7 is behaving really strangely
// if index == 1, nothing returns from getElementsByName
// if index == 0, everything is returned. zzz
alert('getting ' + 'hobby['+ index +']');
inputs = document.getElementsByName('hobby['+ index +']');
for(var i = 0; i < inputs.length; i++)
{
alert(inputs[i].name);
} Last edited by jakesee : Jul 9th, 2008 at 4:18 am. Reason: More CODE
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation:
Rep Power: 3
Solved Threads: 16
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
•
•
Join Date: Jul 2008
Posts: 33
Reputation:
Rep Power: 1
Solved Threads: 1
But the implementation between IE and FF doesn't seem like standard.. =( something so simple they have to make it so complicated. I have been tweaking my code so many times just to accomodate both browsers....
[ I further discovered something even more devastating, which I think I should document here in case someoe else needs it. ]
Following the above issue that IE7 cannot reference re-named form fields, BUT able to form.submit the renamed fields, I decided to do only change the names just before submit so I did this:
//////////////////////////////////////
// In IE7,
// expecting an array of five <select>
var hobbies = document.getElementsByName('hobby');
for(var i = 0; i < hobbies.length; i++)
{
// hobbies.length == 5
hobbies[i].name = 'hobby[' + i + ']';
// hobbies.length == 5
}
//////////////////////////////////////
// In FF2
// apparently a reference to the function result is returned
var hobbies = document.getElementsByName('hobby');
for(var i = 0; i < hobbies.length; i++)
{
// hobbies.length == 5
hobbies[i].name = 'hobby[' + i + ']';
// hobbies.length == 4
}
//////////////////////////////////////
// As such, In FF, I have to do
var hobbies = document.getElementsByName('hobby');
num_hobbies = product_ids.length;
or(var i = 0; i < num_hobbies ; i++)
{
hobbies[0].name = 'hobby[' + i + ']';
// re-retrieve objects
var hobbies = document.getElementsByName('hobby');
}
// However, this code does not work in IE7!
// IE7 doesn't know about the changed names
// and subsequent getElementsByName() will not work properly!ARGHHHH... I'm stucked!
Last edited by jakesee : Jul 10th, 2008 at 4:33 am.
•
•
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation:
Rep Power: 3
Solved Threads: 16
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
Similar Threads
- javascript variable issue (JavaScript / DHTML / AJAX)
- drop down list selected index change doesn't work in mozilla (ASP.NET)
- Change function in bottom (JavaScript / DHTML / AJAX)
- javascript and css (JavaScript / DHTML / AJAX)
- xhtml-javascript taxtarea problem (JavaScript / DHTML / AJAX)
- Javascript browser detection (and then if IE activate script) (JavaScript / DHTML / AJAX)
- disable pager link buttons (ASP.NET)
- javascript to add select button (JavaScript / DHTML / AJAX)
- Dynamically modifying the CSS attributes through Javascript (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: javascript intro in pdf
- Next Thread: Form Validation



Linear Mode