Home > Enterprise >  Select dropdown value on click of radio button
Select dropdown value on click of radio button

Time:01-22

I have one drop-down and two radio buttons, there are two values in both:

jQuery('input[type=radio][name=radio_type]').change(function(e) {
  var value = jQuery(e.target.value);
  jQuery('select[name="optType"] option:selected').attr("selected", null);
  jQuery('select[name="optType"] option[value='   value   ']').attr("selected", "selected");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" value="1" name="radio_type">
<input type="radio" value="2" name="radio_type">

<select name="optType" id="optType" >
  <option value="0">Select</option>
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</select>

I want to select the correct option from the drop-down – the option that has the same value as the radio – on clicking the radio button; for example: if I the click radio button with a value of 1, it should select the drop-down option with the value 1.

I'm using the above script but it is not working.

CodePudding user response:

No need to wrap the e.target.value to jQuery at line 2 ie. jQuery(e.target.value).

use - var value = e.target.value;

It will work.

  •  Tags:  
  • Related