Jump to content

select multiple option from javascript function


michaelfurey

Recommended Posts

Starting from a PHP script I would like to select some options of a multiple select... but I don't know how.

 

This is my html code:


<select id="pairlang[]" name="pairlang[]" multiple size=3>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<input type="submit" value="save allllll" name="update" class="great" onclick="pairSave()">

This is my js code:

function pairSave() {
    var newJs = <?php echo json_encode($pair); ?>;
        alert(newJs);
}

The output of the alert is an array:

volvo,saab

Someone knows how to select the options volvo and saab with the function pairSave?

Link to comment
Share on other sites

You could use getElementById() to get the selection box. Then loop through the options to see which are selected. To see what JavaScript gets, you could try something like this:

function pairSave() {
    var selectionBox = document.getElementById("pairlang[]");
    console.log(selectionBox);
    console.log(selectionBox.options);
}
 
Note that console.log() sends the information to your browser's developer console. The directions for accessing the console will depend on the browser you use. Those directions can be easily found through Google.  :happy-04:
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.