With the following html code:
<select id="radius" class="form-control">
<option value="1">1 km</option>
<option value="2">2 km</option>
<option value="3">3 km</option>
<option value="4">4 km</option>
<option value="5" selected="selected">5 km (default)</option>
<option value="10">10 km</option>
<option value="15">15 km</option>
<option value="20">20 km</option>
</select>
I created the following dropdown menu:
When the user selects one of the distances, the app will do some business logic based on the choice (for now it just outputs an alert message in case option 1 is selected). If the user doesn't choose a distance, it defaults to 5 km.
In my javascript file I added the following code:
function setRadius(){
var radius = document.getElementById("radius");
var selectedValue = radius.options[radius.selectedIndex].value;
if (selectedValue == "1"){
alert("one");
}
}
$("#radius").on("mouseout", function(){
setRadius();
});
The idea behind this code is as follows:
If the user chooses a distance, he/she clicks on the dropdown menu, chooses the distance and then moves the mouse away from the menu. That is when the the setRadius() function is supposed to get triggered. However, the code above does not work. No alert message is generated when a distance of 1 km is selected (and the mouse moves away from the dropdown menu).
Why is t not working?
**************************UPDATE************************************
As a result of the comments I changed my code to this:
$(document).ready(function(){
$("#radius").on("change", function(){
if (this.value == "1"){
alert("one");
}
});
});
However, it still doesn't work.
********************UPDATE2***************************
if I change the code to this:
$(document).ready(function(){
$("#radius").on("change", function(){
console.log(this.value);
});
});
I get the following error message in the console:
WHY? I just don't understand.

