Home > OS >  How to set multiple values to list value in JavaScript
How to set multiple values to list value in JavaScript

Time:11-19

I want to select 2 options into droplist that were given by the user variables. The code below can select just one variable, so what I can do to select more than one. These variables are already on my list.

<script type="text/javascript">
    $(document).ready(()=>{
       var  value1 ="<?php echo $name1; ?>";
       var  value2 ="<?php echo $name2; ?>";
         $('#select option[value="'   value1  '"]').attr("selected", "selected");
         $('#select option[value="'   value2   '"]').attr("selected", "selected");
       });     
 </script>

CodePudding user response:

Simply you could use the val function in jquery

<script type="text/javascript">
   $(document).ready(()=>{
      var  value1 ="<?php echo $name1; ?>";
      var  value2 ="<?php echo $name2; ?>";
        $('#select').val([value1, value2]);
      });     
</script>
  • Related