IE Issue with Replacing Name of Input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rdlauer@gmail.com

    IE Issue with Replacing Name of Input

    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_ro les" value="<input type='checkbox'
    name='replace_m e' value='1'><inpu t type='checkbox' name='replace_m e'
    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.innerHTM L = document.myform .hidden_roles.v alue;

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

    replace_roles = document.myform .hidden_roles;
    replace_roles_l ength = hidden_roles.le ngth;

    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
    rdlauer@gmail.c om

  • RobG

    #2
    Re: IE Issue with Replacing Name of Input

    rdlauer@gmail.c om wrote:[color=blue]
    > 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_ro les" value="<input type='checkbox'
    > name='replace_m e' value='1'><inpu t type='checkbox' name='replace_m e'
    > 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.innerHTM L = document.myform .hidden_roles.v alue;
    >
    > So far so good. I then run this javascript to replace the NAMES of
    > those checkboxes:
    >
    > replace_roles = document.myform .hidden_roles;[/color]

    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.
    [color=blue]
    > replace_roles_l ength = hidden_roles.le ngth;
    >
    > 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.[/color]

    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_ro les" value="1,2">


    [SCRIPT]

    // Optional to remove the contents of 'mycell':
    while( mycell.firstChi ld) {
    mycell.removeCh ild(mycell.firs tChild);
    }

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



    --
    Rob

    Comment

    Working...