Jump to content

select multiple option from javascript function


michaelfurey
 Share

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?

Edited by michaelfurey
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

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.