michaelfurey Posted May 19, 2017 Share Posted May 19, 2017 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 More sharing options...
cyberRobot Posted May 19, 2017 Share Posted May 19, 2017 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.