#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    21
    Rep Power
    0

    Select all items in list box onsubmit


    Hi,

    I want to select all items in this listbox onsubmit of the form.

    Code:
    <script type="text/javascript">
    function CheckAll(){
    
      var len = (document.forms['repeat'].['tester'].length + 1) //this gets the 
                           //amount of options you have in the select field
    
       //now go throught the array to select all the options
    
       for (i = 0; i < len; i++){
             document.forms['repeat'].['tester'].Options[i].value = selected
            }
      }
    
    </script>
    Code:
    <?php
    
    echo "<form id=\"repeat\" name=\"repeat\" action=\"questionsubmit.php\" method=\"post\" onsubmit=\"CheckAll()\">";	
    
    	$tagvalues = $_POST['Taglist'];
    
    echo "<select multiple name='tester[]' id='tester'>";
    	foreach ($tagvalues as $a){
        echo "<option value='$a'>$a</option>";
    	}
    echo "</select>";
    
    ?>
    This isn't working. I know I shouldn't be echoing as much as I have here - I'll change that later. Any help is appreciated.

    Daniel
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,044
    Rep Power
    119
    You would do it like this:
    Code:
    function CheckAll()
    {
    var sel = document.getElementById("tester").getElementsByTagName("option");
     for(i=0;i<sel.length;i++){
      sel[i].selected = true;
     }
    }
    Or... you could do it like so:
    Code:
    function CheckAll()
    {
    var sel = document.getElementById("tester").getElementsByTagName("option");
     for(i=0;i<sel.length;i++){
      sel[i].setAttribute("selected","selected");
     }
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    21
    Rep Power
    0

    Talking Solved


    Thank you for your reply. I have now realised I do not need to do it this way - it is a bit messy! Instead I used a loop to produce hidden fields containing the array values!

    Thanks

IMN logo majestic logo threadwatch logo seochat tools logo