Results 1 to 2 of 2

Thread: cross browser select length

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default cross browser select length

    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.

    thanks

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <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">
    jQuery(function($){
    	alert($('select')[0].options.length);
    });
    </script>
    </head>
    <body>
    <select>
    </select>
    </body>
    </html>
    Alerts '0'.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •