By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

IE Issue with Replacing Name of Input

P: n/a
I've just run into a strange problem with IE 6 and I'm wondering if
anyone else has seen the same:

On my form I have a hidden field that contains the HTML for a series of
checkboxes:

<input type="hidden" name="hidden_roles" value="<input type='checkbox'
name='replace_me' value='1'><input type='checkbox' name='replace_me'
value='2'>"

Weird, I know, just stay with me...

I load the value of that hidden field into a new table cell created
with:

mycell.innerHTML = document.myform.hidden_roles.value;

So far so good. I then run this javascript to replace the NAMES of
those checkboxes:

replace_roles = document.myform.hidden_roles;
replace_roles_length = hidden_roles.length;

for (i=0; i<replace_roles_length; i++) {
//rename those checkboxes
replace_roles[i].name = "whatever" + counter;
//the counter will change each time this is run
}

This WORKS. It DOES rename the checkboxes. HOWEVER, if I run this whole
process again on the same page (I'm adding another series of checkboxes
somewhere else) it's as if IE doesn't remember that it already renamed
those first checkboxes, so it renames ALL of them again.

This all works fine in Firefox. Any ideas? I know this is a weird one.

Thanks in advance!
Rob
rd*****@gmail.com

Oct 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
rd*****@gmail.com wrote:
I've just run into a strange problem with IE 6 and I'm wondering if
anyone else has seen the same:

On my form I have a hidden field that contains the HTML for a series of
checkboxes:

<input type="hidden" name="hidden_roles" value="<input type='checkbox'
name='replace_me' value='1'><input type='checkbox' name='replace_me'
value='2'>"

Weird, I know, just stay with me...

I load the value of that hidden field into a new table cell created
with:

mycell.innerHTML = document.myform.hidden_roles.value;

So far so good. I then run this javascript to replace the NAMES of
those checkboxes:

replace_roles = document.myform.hidden_roles;
That will return a reference to 'hidden_roles', which is the hidden
input. The name of the two new ones is 'replace_me'. So maybe:

replace_roles = document.myform.replace_me;
Which should return a collection of the elements named 'replace_me', but
if there is only one it will return a reference to the element. If
there is any doubt about whether you'll get a collection or not, test
the length before assuming it exists.
replace_roles_length = hidden_roles.length;

for (i=0; i<replace_roles_length; i++) {
//rename those checkboxes
replace_roles[i].name = "whatever" + counter;
//the counter will change each time this is run
}

This WORKS. It DOES rename the checkboxes. HOWEVER, if I run this whole
process again on the same page (I'm adding another series of checkboxes
somewhere else) it's as if IE doesn't remember that it already renamed
those first checkboxes, so it renames ALL of them again.


Why not use DOM and replace the names as you go? The value of
hidden_roles can be a separated list of values (I've used a comma but
any convenient character will do):

[HTML]
<input type="hidden" name="hidden_roles" value="1,2">
[SCRIPT]

// Optional to remove the contents of 'mycell':
while( mycell.firstChild) {
mycell.removeChild(mycell.firstChild);
}

// Add the new checkboxes
var newCB;
var values = document.myform.hidden_roles.value.split(',');
for (var i=0, j=values.length; i<j; ++i){
newCB = document.createElement('input');
newCB.type = 'checkbox';
newCB.name = 'whatever' + counter;
newCB.value = values[i];
myCell.appendChild(newCB);
}

--
Rob
Oct 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.