Reputation: 693
setDefaultValue function is called when operation is changed on the page so when one operation is done i want to set dropDown value to JavaScript
using Jquery thats not happening with below code , what is implemented wrong ?
dropdown text should say JavaScript
main.html
<div style="text-align: right;margin: 5px;margin-left: 45%">
<select name="dropDownSelect" onchange="handleDropDown(this.value)">
<option value="JS" selected="selected">JavaScript</option>
<option value="TS">TypeScript</option>
</select>
</div>
main.js
function setDefaultValue () {
$("#dropDownSelect").val('JavaScript');
}
Upvotes: 0
Views: 8757
Reputation: 11
Try this, if you just need to change the selected value of the drop down:
$("select[name='dropDownSelect'] option[value='TS']").attr('selected','selected');
If there is an event bind on the drop down change, or any function is called, try this:
$("select[name='dropDownSelect'] option[value='TS']").attr('selected','selected');
$("select[name='dropDownSelect']").trigger('change');
Upvotes: 1
Reputation: 468
Try to do this like
<select name="myselect" id="myselect">
<option value="1">Value one</option>
<option value="2">Value two</option>
<option value="3">Value three</option>
</select>
$("#myselect option[value=2]").attr('selected', 'selected');
Or
<script type="text/javascript">
function addOption() {
optionText = 'Value two';
optionValue = 'Value two';
$('#select1').append(`<option value="${optionValue}">
${optionText}
</option>`);
}
</script>
Upvotes: 1