View Full Version : cross browser select length

06-01-2011, 03:53 AM
hey guys
im looking for a cross browser way to check if the length of a html select element is zero.
im a little confused as to if i can use (along with jquery) $('#selectElem')[0].options.length == 0 because i read on w3c that select elements always have one item, meaning are never zero.

thoughts and knowledge on this is appretiated.


06-01-2011, 04:23 AM
What you read on the w3c is the standard for HTML 4.01 and for XHTML. However, in HTML 5 a select may have 0 options.

Regardless, these are simply the standards. What any given select has in the way of options in actual practice may happen to vary from 0 to whatever and although there are perhaps better ways, you can test the length of its options node list in just the way you propose.

Whenever you are in doubt of such a thing, you can always try it out. For example (valid HTML 5):

<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">

Alerts '0'.