how to refer "this Value of the SELECT component in javascript?

G

Grant Wagner

It's usually best to put your query in the body of the post, not
in the subject, it gives the replier something to quote and
maintains the chain of posts.

If by "this Value of the SELECT component" you mean the value of
the currently selected <option> in a <select size="1">, the way
to obtain the value from outside the form is:

var selectObject =
document.forms['formName'].elements['selectName'];
var theValue =
selectObject.options[selectObject.selectedIndex].value;

from the <select> itself:

<select
onchange="alert(this.options[this.selectedIndex].value);">

From the FAQ for this newsgroup: <url:
http://jibbering.com/faq/#FAQ4_13 />

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available
at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
P

PatD

In a land long ago, in a time far away
Grant Wagner said:
from the <select> itself:

<select onchange="alert(this.options[this.selectedIndex].value);">

I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?


Yours
P
 
M

Mick White

PatD said:
I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?
That will work in every browser I have.
Mick
 
L

Lee

Mick White said:
I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?
That will work in every browser I have.

Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.
 
L

Lasse Reichstein Nielsen

Lee said:
<select onChange="alert(this[this.selectedIndex].value)">
That will work in every browser I have.

Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.

I'd say it was less standard, if anything. The tradition of making
intrinsic event handlers methods of the object they are on will make
"this" always refer to that object. Omitting "this." depends on the
browser making the properties of the object available directly (as by
"with (this) { ... }"). Browsers differ in how many properties they
make available - some also make the properties of the document and the
enclosing form available, others don't. I would feel much less certain
that all future browsers will implement this the same way.

I can't find a current browser where it makes a difference, though.

What *is* more standard is:
"alert(this.value);"
I.e., taking the value directly from the select element. However, that
is a more recent standard, and not all old browsers support it.

/L
 
M

Mick White

Lee said:
Mick White said:
PatD wrote:

I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?

That will work in every browser I have.


Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.
Fails in NN4, though.
Mick
 
L

Lee

Mick White said:
Mick White said:
PatD wrote:



I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?


That will work in every browser I have.


Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.
Fails in NN4, though.

Nope. That's where I first started using it.
it's "this.value" that fails in NN4.
 
R

Richard Cornford

Lasse said:
Lee said:
Now test "alert(options[selectedIndex].value)"
I can't find a current browser where it makes a difference,
though.
<snip>

It fails on Opera 6 (current on some phones apparently) and at least one
other embedded browser (NetFront?) that I have seen. I think I would
stick with:-

this.options[this.selectedIndex].value

- as it is HTML DOM standard and nobody has named a context in which it
will not work.

Richard.
 
G

Grant Wagner

PatD said:
In a land long ago, in a time far away
Grant Wagner said:
from the <select> itself:

<select onchange="alert(this.options[this.selectedIndex].value);">

I've been under the impression that this can be (savely)
shortened to:
<select onChange="alert(this[this.selectedIndex].value)">

Any browsers that wouldn't get this?

Yours
P

I wouldn't take the risk to save 8 characters. If I used the above code
in enough places to warrant saving 8 characters, I'd write a function
and save much much, much more space (I might even attach the events
dynamically at load time rather then code them as HTML attributes).

Back to the original question however. A <select> has a collection of
<option> objects, each of which has a "value" attribute (as well as
other attributes), it also has a property called "selectedIndex". It
seems reasonable to me to access the "value" attribute of the <option>
object at the selectedIndex position within the collection. I use:

this.options[this.selectedIndex].value

because it is exactly what I intended, for the same reason I choose to
use:

document.forms['formName'].elements['elementName'] over
document.forms['formName']['elementName'] (or worse still:
document['formName']['elementName']).

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
M

Matt Kruse

Grant said:
this.options[this.selectedIndex].value

It should be noted that this will fail under at least two situations:
1) If there are no options at all in the select element
2) If there are no options selected

Neither of these will usually occur in an onChange event, except for a
multiple select list. Unless there are browsers which allow for unselecting
every option in a select-one list, but Ive never seen one.
 
P

PatD

In a land long ago, in a time far away

[cut: almost everything]
[left: bare minimum to understand what's going on]
PatD said:
<select onchange="alert(this.options[this.selectedIndex].value);">
<select onChange="alert(this[this.selectedIndex].value)">
I wouldn't take the risk to save 8 characters. If I used the above code
this.options[this.selectedIndex].value
document.forms['formName'].elements['elementName'] over
document.forms['formName']['elementName'] (or worse still:


Interesting threat. Who would have thought so at its beginning? :)

Well, my personal favorite is:
this.options[this.selectedIndex].value

and, I'm slowly changing from
document.forms['formName']['elementName']
to
document.forms['formName'].elements['elementName']


Hm, I guess that some day, someone will start a threat on whether
"document.stuff..."
should not be replaced with
"window.document.stuff..."


Until then, see you, and thanks for all the feedback


Yours
P
 
G

Grant Wagner

PatD said:
In a land long ago, in a time far away

[cut: almost everything]
[left: bare minimum to understand what's going on]
PatD said:
<select onchange="alert(this.options[this.selectedIndex].value);">
<select onChange="alert(this[this.selectedIndex].value)">
I wouldn't take the risk to save 8 characters. If I used the above code
this.options[this.selectedIndex].value
document.forms['formName'].elements['elementName'] over
document.forms['formName']['elementName'] (or worse still:

Interesting threat. Who would have thought so at its beginning? :)

Well, my personal favorite is:
this.options[this.selectedIndex].value

and, I'm slowly changing from
document.forms['formName']['elementName']
to
document.forms['formName'].elements['elementName']

Hm, I guess that some day, someone will start a threat on whether
"document.stuff..."
should not be replaced with
"window.document.stuff..."

Until then, see you, and thanks for all the feedback

Yours
P

Well, I tend to use "window.location.href", but I do not use
"window.document.forms...", nor do I use "window.alert();" although
technically, "location", "document" and "alert()" are all members of the
(usually) omni-present "window" global object in a Web browser.

I'm not sure why I do this, although some of it has to do with the fact that
"location" is a magical object, in that assigning a value to it directly
changes it's "href" property (or, if you prefer, "location" is a magical
property of "window", such that changing it changes window.location.href).
As a result, "location = 'someurl';" is perfectly valid Javascript that
appears to be a simple variable assignment, but in reality can navigate a
Web browser to a new page. Worse yet "var location = 'someurl';" outside of
any function does _not_ navigate the browser to a new page, and stomps all
over the ability to do so via window.location.href (in fairness, the same
could be said of "var document = 'whatever';" or "var alert = 'stuff';").

In the end, I would have been much happier if Javascript authors were
_required_ to use "location.href = 'someurl';" because that is what you are
doing, changing the "href" property of the global Location object. The fact
that the original design of the browser DOM allows these magical objects
with their mysterious behavior breaks the very concept of OO within the
language.

Assigning a string directly to window.location (window.location =
'http://www.yahoo.com';) should remove the reference to the global Location
object and make it eligible for garbage collection, replacing it with a
reference to a String object containing "http://www.yahoo.com", but that's
not what happens in most (all?) browers.

--
| Grant Wagner <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-developer/upgrade_2.html
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Lee said:
<select onChange="alert(this[this.selectedIndex].value)">
That will work in every browser I have.

Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.

I'd say it was less standard, if anything. [...]

It should work in a standards compliant browser and it works in
Mozilla/5.0. "this" is part of the scope chain, so it does not
need to be mentioned explicitely.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Richard said:
Lasse said:
Lee said:
Now test "alert(options[selectedIndex].value)"

I can't find a current browser where it makes a difference,
though.

<snip>

It fails on Opera 6 (current on some phones apparently) and at least one
other embedded browser (NetFront?) that I have seen.

Those browsers should be considered borken or
at least not standards compliant in this regard.
I think I would stick with:-

this.options[this.selectedIndex].value

- as it is HTML DOM standard [...]

Non sequitur. Following the scope chain specification is
not less standard. See ECMAScript 3 (2000), section 10.1.4.


PointedEars
 
R

Richard Cornford

Thomas said:
Richard said:
<snip>

It fails on Opera 6 (current on some phones apparently) and at least
one other embedded browser (NetFront?) that I have seen.

Those browsers should be considered borken or
at least not standards compliant in this regard.
Nonsense.
I think I would stick with:-

this.options[this.selectedIndex].value

- as it is HTML DOM standard [...]

Non sequitur. Following the scope chain specification is
not less standard. See ECMAScript 3 (2000), section 10.1.4.

The ECMA 262 3rd edition scope chain specification has nothing to do
with the behaviour under discussion. It describes how scope chins are
defined/constructed. It says nothing about what objects will appear on
the scope chins of intrinsic event handlers beyond the implication that
the global object will be at the end of them.

Richard.
 
R

Richard Cornford

Thomas said:
Lasse said:
Lee said:
<select onChange="alert(this[this.selectedIndex].value)">
That will work in every browser I have.

Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.

I'd say it was less standard, if anything. [...]

It should work in a standards compliant browser and it works in
Mozilla/5.0. "this" is part of the scope chain, so it does not
need to be mentioned explicitely.

You are falling into the trap of assuming that because Mozilla is the
most standard compliant browser then what Mozilla actually does is
standard. The custom scope chain that is built by Gecko browsers is
unique in terms of the object on the scope chain and the rules used to
construct it. Indeed, experimentation in this area suggests that none of
the browsers that provide custom scope chains for browser built
intrinsic event handling functions provides the same scope chain, or
uses the same rules for its construction, as any other. This is
behaviour that is not standardised.

Richard.
 
L

Lee

Richard Cornford said:
Lasse said:
Lee writes:
<select onChange="alert(this[this.selectedIndex].value)">
That will work in every browser I have.

Now test "alert(options[selectedIndex].value)"

which is slightly shorter and probably slightly more standard.

I'd say it was less standard, if anything. [...]

It should work in a standards compliant browser and it works in
Mozilla/5.0. "this" is part of the scope chain, so it does not
need to be mentioned explicitely.

You are falling into the trap of assuming that because Mozilla is the
most standard compliant browser then what Mozilla actually does is
standard. The custom scope chain that is built by Gecko browsers is
unique in terms of the object on the scope chain and the rules used to
construct it. Indeed, experimentation in this area suggests that none of
the browsers that provide custom scope chains for browser built
intrinsic event handling functions provides the same scope chain, or
uses the same rules for its construction, as any other. This is
behaviour that is not standardised.

Some browsers may include additional objects in the scope chain,
but any that doesn't include "this" is pretty clearly broken.
 
L

Lasse Reichstein Nielsen

Lee said:
Some browsers may include additional objects in the scope chain,
but any that doesn't include "this" is pretty clearly broken.

If by "broken" you mean: will make a significant number of existing
pages break, then I'll have to agree. I wouldn't mind if the scope
chain hack was removed, though.

/L
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Those browsers should be considered borken or
at least not standards compliant in this regard.

There is no standard for this.
I think I would stick with:-

this.options[this.selectedIndex].value

- as it is HTML DOM standard [...]

Non sequitur. Following the scope chain specification is
not less standard. See ECMAScript 3 (2000), section 10.1.4.

Following *a* scope chain is part of ECMAScript. The specific scope
chain used for intrinsic event handlers is not part of any standard
(if any existed, it *would* be the W3C HTML DOM), and goes against
normal behavior of closures by having unrelated DOM objects in the
scope chain and not just variable objects created by function
calls.

/KL
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top