Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Returning <select> result as array... but...

    I have a multiple select box in my form with "select all" and "select none" buttons below it. I have named the select box "country_id" which is used in the "select all" and "select none" javascript functions. This is fine.

    The problem is that I need PHP to recognise the selected items as an array and hence want to call the box "country_id[]". However when I do this my javascript functions no longer work.

    How can I change my javascript functions so that they work with a select box name which ends in []? I have tried a few changes to the javascript but can not get it to work. Below is the code without the [] name.

    Select Box:
    Code:
    <select name="country_id" size="10" multiple>
      <option value="value">name</option>
      ...
    </select>
    Javascript Functions
    Code:
    <script>
    function selAll(_v) {
    for(var i=0;i<document.delivery.country_id.length;i++)
      document.delivery.country_id[i].selected=_v;
    }
    function selVal(_v) {
    for(var i=0;i<document.delivery.country_id.length;i++)
      if (document.delivery.country_id[i].value==_v) {
    		document.delivery.country_id[i].selected=true;
    	} else {
    		document.delivery.country_id[i].selected=false;
    	}
    }
    function selInv() {
    for(var i=0;i<document.delivery.country_id.length;i++)
      if (document.delivery.country_id[i].selected==false) {
    		document.delivery.country_id[i].selected=true;
    	} else {
    		document.delivery.country_id[i].selected=false;
    	}
    }
    </script>
    eTheory - the theory of revolution

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    400
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function selAll(_v) {
      for(var i=0;i<document.forms['delivery']['country_id[]'].length;i++)
        document.forms['delivery']['country_id[]'][i].selected=_v;
      }
    Note that using brackets in a name or id is illegal. You can generally get away with it by using array-notation for object indexing, but be on the lookout for unintended side-effects.
    Last edited by Harry Armadillo; 07-07-2005 at 06:29 AM. Reason: spellin'

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Brilliant, thanks for that hemebond.
    eTheory - the theory of revolution

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    What Harry used is the square bracket notation. You need to learn it as it is more flexible than the dot notation. See my sig for more info.

  • #6
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Will check it out, thanks.
    eTheory - the theory of revolution


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •