User Name Password Register
DaniWeb IT Discussion Community
All
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,989 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,217 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: 1988 | Replies: 17 | Solved
Reply
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

how to change name attributes with Javascript

  #1  
Jul 8th, 2008
Hi,

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]";
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: how to change name attributes with Javascript

  #2  
Jul 8th, 2008
This may help u:

  1. var inputs = document.getElementsByTagName("input");
  2. var i;
  3. for(i=0; i < inputs.length; i++)
  4. inputs[i].name="my_name";
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

Re: how to change name attributes with Javascript

  #3  
Jul 9th, 2008
Thanks that was very helpful!

But just to confirm, must I always start with document.xxxx? can't I use other objects like newRow?
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: how to change name attributes with Javascript

  #4  
Jul 9th, 2008
there are many DOM function to add/del new rows of to create objects. Better google!!
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

Re: how to change name attributes with Javascript

  #5  
Jul 9th, 2008
Originally Posted by Luckychap View Post
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
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: how to change name attributes with Javascript

  #6  
Jul 9th, 2008
document.getElementsByName('hobby[1]')[0];

is not standard function avoid to use this.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,775
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 330
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: how to change name attributes with Javascript

  #7  
Jul 9th, 2008
> is not standard function avoid to use this.

On the contrary, it is.
"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."
Reply With Quote  
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

Re: how to change name attributes with Javascript

  #8  
Jul 10th, 2008
Originally Posted by ~s.o.s~ View Post
> is not standard function avoid to use this.

On the contrary, it is.


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.
Reply With Quote  
Join Date: Aug 2006
Location: Noida, India
Posts: 152
Reputation: Luckychap is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 16
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Junior Poster

Re: how to change name attributes with Javascript

  #9  
Jul 10th, 2008
Instead of changing name can't you change id of the element. This may help in both IE and FF.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Join Date: Jul 2008
Posts: 33
Reputation: jakesee is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
jakesee jakesee is offline Offline
Light Poster

Re: how to change name attributes with Javascript

  #10  
Jul 10th, 2008
I need to change the names to

hobby[0], hobby[1], hobby[2], hobby[3] ...

so that I can pass POST the values to a PHP script...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC