How do you arbitrarily set a value in a dropdown menu using jquery

if I have a dropdown with the following html:

 <select id="myDropdown" name="myDropdown">
<option value="6">Six</option>
<option value="5">Five</option>
<option value="3">Three</option>
<option value="1">One</option>
</select>

How can I change this drop-down menu to a specific selected value after clicking a button?

+3
source share
2 answers

If your button id="myButton", you can do it like this:

$("#myButton").click(function() {
  $("#myDropdown").val(1); //1 being whatever value you want to set
});
+2
source

try it

  jQuery(document).ready(function(){
      $(".mybuttonclass").click(function() {
           $(".myDropdownclass").val(value for that dropdown items);  
  });
  });
+1
source

Source: https://habr.com/ru/post/1738912/


All Articles