In this example, we have a drop-down list with the id and a button with the id. When the button is clicked, the method is used to change the selected value of the drop-down list to 'option2'.
It's worth noting that the $().ready() function is used to ensure that the DOM is fully loaded before manipulating it. Additionally, we include a reference to the jQuery library from a CDN. You can customize this approach to fit your specific use case.",
"dateCreated": "2023-04-28 00:00:00",
"upvoteCount": 0,
"url": "https://www.arrayoverflow.com/question/change-the-selected-value-of-a-drop-down-list-with-jquery/1030#2142",
"author": {
"@type": "Person",
"name": "web-api"
}
}
]
}
}
To change the selected value of a drop-down list with jQuery , you can use the val() method, as demonstrated in this example:
<select id="my-select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
<button id="change-btn">Change selection</button>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#change-btn').click(function() {
// Change the selected value of the drop-down list to "option2"
$('#my-select').val('option2');
});
});
</script>
In this example, we have a drop-down list with the id my-select, and a button with the id change-btn . When the button is clicked, the val() method is used to change the selected value of the drop-down list to "option2".
It's worth noting that the $( document ).ready() function is used to ensure that the DOM is fully loaded before manipulating it. Additionally, we include a reference to the jQuery library from a CDN. You can customize this approach to fit your specific use case.