I have a small jQuery function which collects data from the server depending on a zipcode input field:
$(document).ready(function(){
$('#id_zipcode').keyup(function() {
var selected = this.value;
if (selected.length > 3) {
$.getJSON(get_zip_factors_url, {
zipcode: selected
}, function(env_data, jqXHR) {
$('#snow_fall').html(env_data[0].snow);
$('#wind_speed').html(env_data[0].wind);
});
}
}).keyup();
});
This works fine now. However, if the user deletes characters the values rendered for #snow_fall and #wind_speed persist. Is there some way I can register this deletion and revert the values to say a ‘not available’ string when the user deletes characters which result in no valid data being returned from the server?
Any help much appreciated.
If you’re looking for zip code values why not change to if(selected.length == 5) and then use the else of the if statement to set snow_fall and wind_speed to “not available”?