It seems that my onchange event works only with my select box when I manually make a selection in my selectbox, however, if I were to programmatically make a selection using a button, it does not appear that the onchange button triggers at all. How can the code below be modified such that functionality can be added to trigger the onchange event from both a manual selection and a the button?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function record_select() {
var x = document.getElementById('comments').value
var y = document.getElementById('cars').value
if (y) {
if (x) {
document.getElementById('comments').value = document.getElementById('cars').value + ' was selected\n\n' + x
}
else {
document.getElementById('comments').value = document.getElementById('cars').value + ' was selected'
}
}
}
function change_select() {
document.getElementById('cars').value = 'audi'
}
</script>
</head>
<body>
<select id="cars" onchange="record_select()">
<option value=""></option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br>
<textarea id="comments" style="width: 400px; height: 50px;"></textarea>
<br>
<input type="button" value="change" onclick="change_select()">
</body>
</html>