By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,567 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,567 IT Pros & Developers. It's quick & easy.

Help Please !! Javascript may inquire the push down menu value, can I inquire the description?

P: n/a
Javascript may inquire the push down menu value, can I inquire the
description?

The following example, the variable($answer) can be get the menu1's
value.
For example, if I select first data, the menu1's value would be 1.
Therefore ($answer) would be 1.
But If want using javascript get the description(ABC),which javascript
command can do it?

e.g.
<script>
function this() {
$answer = document.form1.menu1.value;
}
</script>
<form name="form1" method="post" action="">
<select name="menu1" onChange="this();">
<option value=1>ABC</option>
<option value=2>DEF</option>
<option value=3>GHI</option>
<option value=4>JKL</option>
</select>
</form>
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"???????J" <an****@tophk.net> wrote in message
news:ed**************************@posting.google.c om...
Javascript may inquire the push down menu value, can I inquire the
description?

The following example, the variable($answer) can be get the menu1's
value.
For example, if I select first data, the menu1's value would be 1.
Therefore ($answer) would be 1.
But If want using javascript get the description(ABC),which javascript
command can do it?

e.g.
<script>
function this() {
$answer = document.form1.menu1.value;
}
</script>
<form name="form1" method="post" action="">
<select name="menu1" onChange="this();">
<option value=1>ABC</option>
<option value=2>DEF</option>
<option value=3>GHI</option>
<option value=4>JKL</option>
</select>
</form>

"this" is a reserved word and should not be the name of a function.

Try the following:

<html>
<head>
<title>not_this.htm</title>
<script language="javascript" type="text/javascript">
<!--
function which(that) {
var answer = that.options[that.selectedIndex].value;
alert(answer);
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select name="menu1" onChange="which(this)">
<option value="1">ABC</option>
<option value="2">DEF</option>
<option value="3">GHI</option>
<option value="4">JKL</option>
</select>
</form>
</body>
</html>

Jul 20 '05 #2

P: n/a
Thanks for your reply,
I using wrong command (this), that may be using other word.
Can you tell me how can I got ABC?
I need using two value (1 and ABC). And I don't want using array
function.
That is because for save memory and speed up download speed.
And I try to optimum my javascript, If I don't using array function, I
shall save around 30k byte space.
"McKirahan" <Ne**@McKirahan.com> wrote in message news:<KKrIb.706552$Fm2.610543@attbi_s04>...
"???????J" <an****@tophk.net> wrote in message
news:ed**************************@posting.google.c om...
Javascript may inquire the push down menu value, can I inquire the
description?

The following example, the variable($answer) can be get the menu1's
value.
For example, if I select first data, the menu1's value would be 1.
Therefore ($answer) would be 1.
But If want using javascript get the description(ABC),which javascript
command can do it?

e.g.
<script>
function this() {
$answer = document.form1.menu1.value;
}
</script>
<form name="form1" method="post" action="">
<select name="menu1" onChange="this();">
<option value=1>ABC</option>
<option value=2>DEF</option>
<option value=3>GHI</option>
<option value=4>JKL</option>
</select>
</form>

"this" is a reserved word and should not be the name of a function.

Try the following:

<html>
<head>
<title>not_this.htm</title>
<script language="javascript" type="text/javascript">
<!--
function which(that) {
var answer = that.options[that.selectedIndex].value;
alert(answer);
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select name="menu1" onChange="which(this)">
<option value="1">ABC</option>
<option value="2">DEF</option>
<option value="3">GHI</option>
<option value="4">JKL</option>
</select>
</form>
</body>
</html>

Jul 20 '05 #3

P: n/a
In article <ed**************************@posting.google.com >, an****@tophk.net
(???????J) writes:

Thanks for your reply,
I using wrong command (this), that may be using other word.
Can you tell me how can I got ABC?
Instead of trying to read the selects .value property, read its .text property:

Re-writing McKirahan's function:

function which(that) {
var answer = that.options[that.selectedIndex].value;
var answerText = that.options[that.selectedIndex].text;
alert("The answer's value is " + answer + "\nAnd its text was " +
answerText);
}
I need using two value (1 and ABC). And I don't want using array
function.
Its not creating an array, its simply reading from the forms array. Its built
in to the browser and whether you read from it or not, its still created.
That is because for save memory and speed up download speed.
And I try to optimum my javascript, If I don't using array function, I
shall save around 30k byte space.


I don't understand how you think that accessing a select's value and text
properties is costing you 30k of space in a page.

If you are trying to write optimum code, then it should work in as many
browsers as possible. Both mine and McKirahan's functions work even in
Netscape4.xx series browsers. The array notation he gave is the proper way to
read a select list, see the FAQ <URL: http://www.jibbering.com/faq/#FAQ4_13 />

Perhaps a little more explanation of what you are trying to accomplish?
--
Randy
Jul 20 '05 #4

P: n/a
On 30 Dec 2003 19:44:31 -0800, ???????J <an****@tophk.net> wrote:
The following example, the variable($answer) can be get the menu1's
value.
For example, if I select first data, the menu1's value would be 1.
Therefore ($answer) would be 1.
But If want using javascript get the description(ABC),which javascript
command can do it?
You use the text property:

document.forms['form_name'].elements['select_name'].options[index].text

This will get the display value of the SELECT option at the specified
index. If you always want the currently selected option, use the
selectedIndex property:

document.forms['form_name'].elements['select_name'].selectedIndex.text

I used the "collections syntax" above. It will work on more browsers than
the equivalent below, so you should get into the habit of using it.

document.form_name.select_name.options[index].text

Please read my other comments below.

Hope that helps,
Mike
<script>
Never do that. You *must* specify the script type (ignore the language
attribute):

<SCRIPT type="text/javascript">
function this() {
The keyword, this, is a reserved word and shouldn't be used for anything
other than its purpose as an operator. Furthermore, an identifier such as
that is a bad idea as it provides no clue as to actions performed by the
function. Always use meaningful identifiers; it's better for you in the
long-run.
$answer = document.form1.menu1.value;
I very much doubt that this variable has any use outside of the scope of
this function. You should consider using the var keyword to declare it in
local scope. You should also use the syntax I showed earlier when
accessing form controls.
}
</script>
<form name="form1" method="post" action="">
The action attribute is required and must be a valid URI. If you are using
the FORM simply to access the controls contained within it, you need not
bother; there are other, better ways.
<select name="menu1" onChange="this();">
<option value=1>ABC</option>
<option value=2>DEF</option>
<option value=3>GHI</option>
<option value=4>JKL</option>
</select>
</form>


If you want to access a control from an event handler, there is a much
simpler way than through a fully-qualified reference. For example:

<SCRIPT type="text/javascript">
function menuHandler( menu ) {
var $answer = menu.selectedIndex.text;
}
</SCRIPT>
...
<!-- No enclosing FORM element -->
<SELECT name="select_menu" onchange="menuHandler(this)">
...
</SELECT>

When the selected option in the SELECT element is changed, the menuHandler
function is passed a reference to the element object, meaning that you no
longer have to reference the object in full.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #5

P: n/a
Michael Winter <M.******@blueyonder.co.invalid> writes:
This will get the display value of the SELECT option at the specified
index. If you always want the currently selected option, use the
selectedIndex property:

document.forms['form_name'].elements['select_name'].selectedIndex.text
I think that was a typo. The selectedIndex property is a number, and
doesn't have a text property.

You prabably meant to write something like:

var select = document.forms['form_name'].elements['select_name'];
... select.options[select.selectedIndex].text

Please read my other comments below.
And I agree with almost all of it, with only minor comments ...
The keyword, this, is a reserved word and shouldn't be used for
anything other than its purpose as an operator.
Even worse, it *can't* be used for anything else. Using it as an
identifier in a declaration (variable or function) is a syntax error.
<form name="form1" method="post" action="">


The action attribute is required and must be a valid URI.


It must be a valid URI *fragment* (otherwise relative URL's wouldn't
be valid), and the empty string is one (just as the often used, but
inferior, "#"). It refers to the current page.
If you are using the FORM simply to access the controls contained
within it, you need not bother; there are other, better ways.
However, if you need to use the page in Netscape 4 (cursed be it),
you need a form element around form controls (otherwise they aren't
rendered at all). Sigh. Otherwise I would agree completely.
var $answer = menu.selectedIndex.text;


menu.options[menu.selectedIndex].text;

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

P: n/a
On Wed, 31 Dec 2003 14:58:27 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Michael Winter <M.******@blueyonder.co.invalid> writes:
This will get the display value of the SELECT option at the specified
index. If you always want the currently selected option, use the
selectedIndex property:

document.forms['form_name'].elements['select_name'].selectedIndex.text


I think that was a typo. The selectedIndex property is a number, and
doesn't have a text property.

You prabably meant to write something like:

var select = document.forms['form_name'].elements['select_name'];
... select.options[select.selectedIndex].text


Thank you for this, and the other corrections; I always appreciated it.
I'm without easy access to my references (though it should be obvious that
selectedIndex does in fact return an index *D'oh*)

<snip>
The keyword, this, is a reserved word and shouldn't be used for
anything other than its purpose as an operator.


Even worse, it *can't* be used for anything else. Using it as an
identifier in a declaration (variable or function) is a syntax error.


I thought it might have been, but I wasn't certain enough to say so.
<form name="form1" method="post" action="">


The action attribute is required and must be a valid URI.


It must be a valid URI *fragment* (otherwise relative URL's wouldn't
be valid), and the empty string is one (just as the often used, but
inferior, "#"). It refers to the current page.


I didn't realise that an empty string was a valid fragment. I always
assumed (due to other parts of poster's messages) that people using empty
strings were being lazy or just plain wrong. However, RFC 1808, Relative
Uniform Resource Locators, makes it quite clear that all parts of a
relative URL are optional.

From Section 2.2, BNF for Relative URLs:

relativeURL = net_path | abs_path | rel_path

net_path = "//" net_loc [ abs_path ]
abs_path = "/" rel_path

[This part:]
rel_path = [ path ] [ ";" params ] [ "?" query ]

and Section 5.2, Abnormal Examples:

An empty reference resolves to the complete base URL:

<> = <URL:http://a/b/c/d;p?q#f>
If you are using the FORM simply to access the controls contained
within it, you need not bother; there are other, better ways.


However, if you need to use the page in Netscape 4 (cursed be it),
you need a form element around form controls (otherwise they aren't
rendered at all). Sigh. Otherwise I would agree completely.

<snip>

Another interesting piece of information to consider.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #7

P: n/a
Thanks for all friend reply.

I already got answer (.text)

Thanks your very much
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.