michaelfurey Posted May 19, 2017 Share Posted May 19, 2017 (edited) 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? Edited May 19, 2017 by michaelfurey Quote Link to comment https://forums.phpfreaks.com/topic/303964-select-multiple-option-from-javascript-function/ 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. Quote Link to comment https://forums.phpfreaks.com/topic/303964-select-multiple-option-from-javascript-function/#findComment-1546629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.