Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22072

Re: Syntax to access the selected option's value

Message-ID <1930014.TA4aT0qaH3@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-11-19 19:55 +0100
Subject Re: Syntax to access the selected option's value
Newsgroups comp.lang.javascript
References <7fc7ae95-94ad-4c65-9596-3a63d245360e@googlegroups.com> <528b9f28$0$9502$9b4e6d93@newsspool1.arcor-online.net>

Show all headers | View raw


Christoph Michael Becker wrote:

> Bart Van der Donck wrote:
>> Say the following code:
>> 
>> <form name="f" method="get"> <select name="myName" id="myID"> <option
>> value="a">a</option> <option value="b">b</option> <option
>> value="c">c</option> </select> </form>
>> 
>> There are two 'official' ways to read out the current selected
>> value:
>> 
>> document.getElementById('myID').value
>> document.f.myName.options[document.f.myName.selectedIndex].value
> 
> It seems reasonable to distinguish between referencing the form element
> and accessing its value.  The latter is explained in this NG's FAQ[1],
> the former in a side note to the FAQ[2].

However, the “official“, i.e. *both* standards-compliant and backwards-
compatible approach is

  var el = document.form["f"].elements["myName"];
  el.options[el.selectedIndex].value

but it only works for el.type == "select-one", and it is necessary to check 
that el.selectedIndex is not out of bounds; at least, that it is not 
negative.

>> But I can't find any reasonable environment where the following
>> fails:
>> 
>> document.f.myName.value
> 
> As explained in the FAQ[1], this would fail under NN4, for instance.

It also fails under certain circumstances in IE/MSHTML.
 
-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Syntax to access the selected option's value Bart Van der Donck <bart@nijlen.com> - 2013-11-19 06:20 -0800
  Re: Syntax to access the selected option's value "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-11-19 08:27 -0800
  Re: Syntax to access the selected option's value Christoph Michael Becker <cmbecker69@arcor.de> - 2013-11-19 18:26 +0100
    Re: Syntax to access the selected option's value Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-19 19:55 +0100
      Re: Syntax to access the selected option's value Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-11-19 19:18 +0000
        Re: Syntax to access the selected option's value Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-19 22:48 +0100
  Re: Syntax to access the selected option's value "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2013-11-19 19:32 +0200
    Re: Syntax to access the selected option's value Bart Van der Donck <bart@nijlen.com> - 2013-11-20 02:40 -0800
      Re: Syntax to access the selected option's value Christoph Michael Becker <cmbecker69@arcor.de> - 2013-11-21 22:27 +0100

csiph-web