Help Please !! Javascript may inquire the push down menu value, can I inquire the description&#65311

?

???????J

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>
 
M

McKirahan

???????J said:
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>
 
?

???????J

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 said:
???????J said:
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>
 
M

Michael Winter

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

Never do that. You *must* specify the script type (ignore the language
attribute):

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.
 
L

Lasse Reichstein Nielsen

Michael Winter said:
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.
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
 
M

Michael Winter

Michael Winter said:
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*)

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

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
 
?

???????J

Thanks for all friend reply.

I already got answer (.text)

Thanks your very much
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top