Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by Philip M View Post
    A select list option cannot have an id.
    Quote Originally Posted by WolfShade View Post
    Options don't have "ID".. text, value.. I think that's it.
    Quote Originally Posted by Philip M View Post
    Yes, I think that has been made clear already. Text, value and selectedIndex.
    This isn't true; option elements can have id attributes. See:

    As far as I can tell, there's no longer a restriction on characters allowed in IDs other than spaces in HTML5, so that would make this solution viable:

    Code:
    <!doctype html>
    <html lang="en-US">
    	<head>
    		<title>Demo Document</title>
    	</head>
    	<body>
    		<select>
    			<option value="">-First Select Color-</option>
    			<option id="1993" value="1993">N:Navy</option>
    			<option id="1997" value="1997">BK:Black</option>
    			<option id="2200" value="2200">O:Orchid</option>
    		</select>
    		<script>
    			function select_option(option_value) {
    				document.getElementById(option_value).selected = true;
    			}
    			select_option("1993");
    		</script>
    	</body>
    </html>
    Logic Ali's solution is a more straightforward (and still standards-compliant) though.
    Last edited by Arbitrator; 03-13-2013 at 07:02 AM. Reason: I fixed several typos.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  2. #17
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Arbitrator View Post
    This isn't true; option elements can have id attributes. See:
    You tell me something. But I have never had any occasion to use an option id.

    But surely an id may not start with a number?
    From the HTML 4 specification: ID and NAME tokens must begin with a letter ([A-Za-z])

    I tested your script and - amazingly it works in HTML4 (no !DOCTYPE)! Why is that?

    I read that HTML5 gets rid of the additional restrictions on the id attribute. The only requirements left — apart from being unique in the document — are that the value must contain at least one character (can’t be empty), and that it can’t contain any space characters.

    Could you devise an example where an option id is really useful?
    Last edited by Philip M; 03-13-2013 at 08:48 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  3. #18
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by Philip M View Post
    You tell me something. But I have never had any occasion to use an option id.
    Well, now you can use them to your heart's content.

    Quote Originally Posted by Philip M View Post
    But surely an id may not start with a number?
    From the HTML 4 specification: ID and NAME tokens must begin with a letter ([A-Za-z])
    In HTML4 and XHTML 1/1.1, there is that restriction, yes. This is a legacy, SGML-based restriction though.

    I can't find anything in the HTML5 specs (WHATWG or W3C) or the spec that those two link to relating to IDs (the WHATWG's DOM Living Standard spec) that indicates that this restriction persists.

    Quote Originally Posted by Philip M View Post
    I tested your script and - amazingly it works in HTML4 (no !DOCTYPE)! Why is that?
    Well, the id attribute for option elements is defined in the HTML4 spec, so it's not really surprising.

    Quote Originally Posted by Philip M View Post
    Could you devise an example where an option id is really useful?
    No, not really, unless maybe it works better than Logical Ali's solution in the situation that Old Pedant mentioned (compatibility with old versions of Firefox). I never said that it was particularly useful, just that it wasn't disallowed.

    Thankfully, there's no need to remember which elements id attributes are allowed on anymore; HTML5 simplifies things by permitting IDs for everything instead of carving out exceptions like HTML4 did.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  4. #19
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Arbitrator View Post

    Well, the id attribute for option elements is defined in the HTML4 spec, so it's not really surprising.
    I was referring to your ids being numbers, said to be illegal in HTML4.

    I have to say that in future I shall still avoid assigning ids which start with an number. That ensures backwards compatibility, and I don't see the restriction as significant. I don't see any reason or situation where it is necessary to use an id starting with a number. But old habits die hard.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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