What is a safe way to check that a listbox is empty in JQuery ? Alternative question, how to check if there is no item actively selected ?
I have tried :
if(... .val() != "null")
also
if(... .val() != null)
as I spotted null value on val() while debugging when the list is empty or nothing is selected, but doesn’t seem to work accurately.
To check if there are
<option>elements inside the<select>element:To check to see if an
<option>inside the<select>has been selected (only works if selected<option>element has avalueattribute)Here is a demo: http://jsfiddle.net/sJdBJ/
Some Docs:
typeof(): https://developer.mozilla.org/en/JavaScript/Reference/Operators/typeof.children(): http://api.jquery.com/children.length: http://api.jquery.com/length