Scott Eldo Scott Eldo - 2 months ago 6
jQuery Question

Activate a dropdown option from another button/llink

I have been struggling to work out how to do this. I have a dropdown on a page that I have no control over code wise -

<select name="dex_item" id="dex_item" onchange="dex_updateItem()">
<option value=1>2016</option>
<option value=4>2017</option>
<option value=5>2018</option>
</select>


I want to create a simple button/link that when clicked activates one of the options. So for example when I click #click then the option value=4 is selected.

I have seen examples similar but can't get anything to work...

Answer

Maybe I misunderstood but to select a value of the the select input you can use the following code :

$("#click").click(function(e) {
  $("#dex_item").val('4');
});

https://jsfiddle.net/Tintin37/fmncst9L/

EDIT

If you want to trigger the onchange event you can do this :

 $(function() {
   $("#click").click(function(e) {
     var select = $("#dex_item");
     select.val('4').trigger("change");
   });
 })

 function dex_updateItem() {
   alert("change");
 }

https://jsfiddle.net/Tintin37/z2bnvuru/