html - Javascript ignore empty dropdown value -
i have been trying multiple ways dropdown search function exclude empty values. example have following code blank lines (ie. value="") allow each country group separate. problem no matter page refreshes when click on these empty values. tried exclude them using if statement, didn't work. can't have count content loaded dynamically. appreciated new javascript.
thanks!
<select id="state"> <option value="">select state/province search</option> <option value=""></option> <option value="http://www.cabcot.com/listings/canada/">canada</option> <option value="http://www.cabcot.com/listings/canada/manitoba/"> - manitoba</option> <option value=""></option> <option value="http://www.cabcot.com/listings/united-states/">united states</option> <option value="http://www.cabcot.com/listings/united-states/california/"> - california</option> </select> <script> document.getelementbyid("state").onchange = function() { if (this.selectedindex!==0 && this.selectedindex!=="" && this.selectedindex!==null) { window.location.href = this.value; } }; </script>
you testing this.selectedindex
, never empty string because value number. test this.value
instead:
document.getelementbyid("state").onchange = function() { if (this.value != "") { window.location.href = this.value; } };
Comments
Post a Comment