Selecting all items in a multiple select: inconsistency with Internet Explorer

Discussion in 'Javascript' started by james.dixon@canberra.edu.au, Sep 7, 2005.

  1. Guest

    Hi

    I was wondering if anyone else had had this problem before (can't find
    anything on the web about it).

    I have three select elements (list boxes - from here on I'll refer to
    them as 'the list boxes'). Users can add and remove items from the
    list boxes.

    When the users are adding and removing items, the list boxes are single
    selection only. However, when the form is submitted, I want ALL items
    to be submitted. So I have written the following script,which ensures
    that all elements in each box are selected when the user submits the
    form. It works by programatically setting the list boxes to multiple,
    and then selecting the controls in them:

    function selectAllInLists()
    {
    var controlArray = new Array();
    controlArray[0] = "bTelephone";
    controlArray[1] = "bEmail";
    controlArray[2] = "bWebsite";

    for(var i=0;i<controlArray.length;i++)
    {
    var listWeb = document.getElementById(controlArray);
    listWeb.multiple = true;

    for(var j=0;j<listWeb.length;j++)
    {
    listWeb[j].selected = true;
    }
    }
    }

    This works fine with Firefox, and most of the time in Internet
    Explorer. However, if the user wants to update an existing record, and
    the list boxes have more than one element (each), and the user makes no
    change to the list boxes (doesn't touch them), THEN only the last item
    in each list box is passed when the form is submitted. Perplexing!!
    I've tried a number of hacks, such as programmatically selecting the
    list box when entering the page, and adding and deleting one
    programmatically. No luck.

    Any pointers would be greatly appreciated!

    Cheers

    James
     
    , Sep 7, 2005
    #1
    1. Advertising

  2. RobG Guest

    Re: Selecting all items in a multiple select: inconsistency withInternet Explorer

    wrote:
    > Hi
    >
    > I was wondering if anyone else had had this problem before (can't find
    > anything on the web about it).
    >
    > I have three select elements (list boxes - from here on I'll refer to
    > them as 'the list boxes'). Users can add and remove items from the
    > list boxes.
    >
    > When the users are adding and removing items, the list boxes are single
    > selection only. However, when the form is submitted, I want ALL items
    > to be submitted. So I have written the following script,which ensures
    > that all elements in each box are selected when the user submits the
    > form. It works by programatically setting the list boxes to multiple,
    > and then selecting the controls in them:
    >
    > function selectAllInLists()
    > {
    > var controlArray = new Array();
    > controlArray[0] = "bTelephone";
    > controlArray[1] = "bEmail";
    > controlArray[2] = "bWebsite";


    This could be written as:

    var controlArray = [
    "bTelephone",
    "bEmail",
    "bWebsite"
    ];

    >
    > for(var i=0;i<controlArray.length;i++)
    > {
    > var listWeb = document.getElementById(controlArray);
    > listWeb.multiple = true;
    >
    > for(var j=0;j<listWeb.length;j++)
    > {
    > listWeb[j].selected = true;
    > }
    > }
    > }
    >
    > This works fine with Firefox, and most of the time in Internet
    > Explorer. However, if the user wants to update an existing record, and
    > the list boxes have more than one element (each), and the user makes no
    > change to the list boxes (doesn't touch them), THEN only the last item
    > in each list box is passed when the form is submitted. Perplexing!!
    > I've tried a number of hacks, such as programmatically selecting the
    > list box when entering the page, and adding and deleting one
    > programmatically. No luck.
    >
    > Any pointers would be greatly appreciated!


    Have you considered writing the values to a hidden input? Then ignore
    whatever values you get from the selects. You can format the value of
    the hidden input to whatever suits your server parser.

    If JavaScript is not available on the client you will get junk, but
    since your form is already dependent on JavaScript perhaps that's not an
    issue for you.


    --
    Rob
     
    RobG, Sep 7, 2005
    #2
    1. Advertising

  3. Matt Kruse Guest

    Matt Kruse, Sep 7, 2005
    #3
  4. Guest

    Thanks Rob, I did as you suggested, and it works. Still a real pain
    though - I wonder if that is an identified bug?
     
    , Sep 7, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cmay
    Replies:
    0
    Views:
    1,180
  2. Replies:
    3
    Views:
    1,039
    Kent Johnson
    Apr 22, 2005
  3. Ed Hauptman
    Replies:
    7
    Views:
    1,100
    Ed Hauptman
    Aug 7, 2009
  4. Gumtree
    Replies:
    3
    Views:
    280
    Matt Kruse
    Nov 10, 2004
  5. John Smith
    Replies:
    1
    Views:
    174
    Evertjan.
    Mar 12, 2005
Loading...

Share This Page