Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Getting the name of selected option
Old 06-13-2005, 01:45 PM Getting the name of selected option
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
I'm trying to get the name (visible text) of the selected option in the drop down... I'm not sure why it's not working. I can get the value, but not the name:

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
function show() {
alert(document.getElementById('series').name + " : " + document.getElementById('series').value);
}
//-->
</script>
	</head>

	<body bgcolor="#ffffff">
		<p><select id="series" size="1">
				<option value="one">first</option>
				<option value="two">second</option>
				<option value="three">third</option>
			</select><input type="submit" name="submitButtonName" onclick="show();"></p>
	</body>

</html>
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
 
Register now for full access!
Old 06-13-2005, 02:37 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
be nice if it were that simple. options does'nt have a "name" property. it is the text property of the selected option you need. So;
Code:
function show() {
var sel_opt = document.getElementById('series').options.selectedIndex;
alert(document.getElementById('series').options[sel_opt].text + " : " + document.getElementById('series').value);
}

you've just got to love javascript for it's nice clean use of OOP syntax. NOT!

it could of course be written as

Code:
alert(document.getElementById('series').options[document.getElementById('series').options.selectedIndex].text + " : " + document.getElementById('series').value);
if you want to be truly convoluted.

btw the code box breaks "selectedIndex" it should be a single word.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-13-2005, 02:48 PM
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Kinda weird that you can grab the value and not the name/text without identifying the selected option by number. But I got it. Thanks.
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 06-13-2005, 03:00 PM
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,520
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
It's all down to the nature of OOP.

the <option>s are child objects of <select> so you need to reference the child object array properties from the parent object.

it almost makes C++ look simple
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Getting the name of selected option
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.31738 seconds with 12 queries