Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-13-2013, 06:53 AM   PM User | #16
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,902
Thanks: 5
Thanked 188 Times in 185 Posts
Arbitrator is on a distinguished road
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.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *

Last edited by Arbitrator; 03-13-2013 at 07:02 AM.. Reason: I fixed several typos.
Arbitrator is offline   Reply With Quote
Old 03-13-2013, 08:36 AM   PM User | #17
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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?
__________________

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.

Last edited by Philip M; 03-13-2013 at 08:48 AM..
Philip M is offline   Reply With Quote
Old 03-13-2013, 09:52 AM   PM User | #18
Arbitrator
Senior Coder

 
Arbitrator's Avatar
 
Join Date: Mar 2006
Location: Splendora, Texas, United States of America
Posts: 2,902
Thanks: 5
Thanked 188 Times in 185 Posts
Arbitrator is on a distinguished road
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.
__________________
Please for the love of god stop making IE. You current "browser"s cause me to cry every day. —Phil *
Arbitrator is offline   Reply With Quote
Old 03-13-2013, 11:19 AM   PM User | #19
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:32 PM.


Advertisement
Log in to turn off these ads.