IE Issue with Replacing Name of Input

R

rdlauer

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.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
(e-mail address removed)
 
R

RobG

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.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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top