SYNCER

SYNCER

HTMLSelectElement - HTMLのselect要素

公開日:

HTMLSelectElementは、HTMLのselect要素が実装するインターフェイスです。

概要

名前
HTMLSelectElement
継承
  1. EventTarget
  2. Node
  3. Element
  4. HTMLElement
  5. HTMLSelectElement
実装
実装するインターフェイスはありません。
IDL
[HTMLConstructor]
interface HTMLSelectElement : HTMLElement {
  [CEReactions] attribute DOMString autocomplete;
  [CEReactions] attribute boolean autofocus;
  [CEReactions] attribute boolean disabled;
  readonly attribute HTMLFormElement? form;
  [CEReactions] attribute boolean multiple;
  [CEReactions] attribute DOMString name;
  [CEReactions] attribute boolean required;
  [CEReactions] attribute unsigned long size;

  readonly attribute DOMString type;

  [SameObject] readonly attribute HTMLOptionsCollection options;
  [CEReactions] attribute unsigned long length;
  getter Element? item(unsigned long index);
  HTMLOptionElement? namedItem(DOMString name);
  [CEReactions] void add((HTMLOptionElement or HTMLOptGroupElement) element, optional (HTMLElement or long)? before = null);
  [CEReactions] void remove(); // ChildNode overload
  [CEReactions] void remove(long index);
  [CEReactions] setter void (unsigned long index, HTMLOptionElement? option);

  [SameObject] readonly attribute HTMLCollection selectedOptions;
  attribute long selectedIndex;
  attribute DOMString value;

  readonly attribute boolean willValidate;
  readonly attribute ValidityState validity;
  readonly attribute DOMString validationMessage;
  boolean checkValidity();
  boolean reportValidity();
  void setCustomValidity(DOMString error);

  [SameObject] readonly attribute NodeList labels;
};
仕様書
https://html.spec.whatwg.org/multipage/forms.html#htmlselectelement

説明

HTMLSelectElementを作成する例です。

JavaScript

// 要素を作成する
var element = document.createElement( "select" ) ;	// <select></select>

// 属性をセットする
element.name = "hoge" ;	// →<select name="hoge"></select>

// option要素を追加する
var optionElement1 = new Option( "選択肢1", "a" ) ;	// <option value="a">選択肢1</option>
element.add( optionElement1 ) ;	// →<select name="hoge"><option value="a">選択肢1</option></select>

// option要素を追加する
var optionElement2 = new Option( "選択肢2", "b" ) ;	// <option value="b">選択肢2</option>
element.add( optionElement2 ) ;	// →<select name="hoge"><option value="a">選択肢1</option><option value="b">選択肢2</option></select>

インデックス番号で、option要素を取得できます。

HTML

<select id="hoge">
	<option value="a">選択肢1</option>
	<option value="b">選択肢2</option>
	<option value="c">選択肢3</option>
	<option value="d">選択肢4</option>
</select>

JavaScript

// 要素を取得する
var element = document.getElementById( "hoge" ) ;	// <select id="hoge"> ... </select>

// インデックス番号から要素を取得する
element[0] ;	// <option value="a">選択肢1</option>
element[1] ;	// <option value="b">選択肢2</option>
element[2] ;	// <option value="c">選択肢3</option>
element[3] ;	// <option value="d">選択肢4</option>

どの項目が選択されているか、手軽に調べることができます。

HTML

<select id="hoge">
	<option value="a">選択肢1</option>
	<option value="b">選択肢2</option>
	<option value="c" selected>選択肢3</option>
	<option value="d">選択肢4</option>
</select>

JavaScript

// 要素を取得する
var element = document.getElementById( "hoge" ) ;	// <select id="hoge"> ... </select>

// 選択されている項目を調べる
element.selectedIndex ;	// 2 (選択肢3が選択されている場合)
element.value ;	// "c" (選択肢3が選択されている場合)

プロパティ

NodeElementHTMLElementのプロパティを利用できます。

HTMLSelectElement.autocomplete

Chrome
×
Firefox
×
Edge
×
IE
×
Safari
9.1+
Opera
× -12
iOS
10.0+
Android
×

autocomplete属性を反映します。

HTMLSelectElement.autofocus

Chrome
Firefox
Edge
IE
10+
Safari
Opera
iOS
Android

autofocus属性を反映します。

HTMLSelectElement.disabled

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

disabled属性を反映します。

HTMLSelectElement.form

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

この要素が関連付けられているform要素を返します。

HTMLSelectElement.labels

Chrome
Firefox
×
Edge
×
IE
×
Safari
5.1+
Opera
iOS
Android
4.0+

関連付けられている全てのlabel要素を、NodeListで返します。

HTMLSelectElement.length

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

所属するoption要素の数を返します。

HTMLSelectElement.multiple

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

multiple属性を反映します。

HTMLSelectElement.name

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

name属性を反映します。

HTMLSelectElement.options

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

所属する全てのoption要素を返します。

HTMLSelectElement.required

Chrome
Firefox
Edge
IE
10+
Safari
5.1+
Opera
11.6+
iOS
Android
4.0+

required属性を反映します。

HTMLSelectElement.selectedIndex

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

選択状態の項目のインデックス番号を返します。

HTMLSelectElement.selectedOptions

Chrome
22+
Firefox
26+
Edge
IE
×
Safari
6.0+
Opera
iOS
6.0+
Android
4.4+

選択状態の全ての項目を、HTMLCollectionで返します。

HTMLSelectElement.size

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

size属性を反映します。

HTMLSelectElement.type

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

単独選択型なら"select-one"、複数選択型なら"select-multiple"を返します。

HTMLSelectElement.validationMessage

Chrome
Firefox
Edge
IE
10+
Safari
5.1+
Opera
iOS
Android

入力内容の検証エラーメッセージを文字列で返します。

HTMLSelectElement.validity

Chrome
Firefox
Edge
IE
10+
Safari
5.1+
Opera
iOS
Android

入力内容の検証結果の情報を表すValidityStateを返します。

HTMLSelectElement.value

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

選択状態の項目の値を返します。

HTMLSelectElement.willValidate

Chrome
Firefox
Edge
IE
10+
Safari
Opera
iOS
Android

要素が、検証候補の場合はtrue、検証候補じゃない場合はfalseを返します。

メソッド

EventTargetNodeElementHTMLElementのメソッドを利用できます。

HTMLSelectElement.add()

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

新しくoption要素、または、optgroup要素を追加します。

HTMLSelectElement.checkValidity()

Chrome
Firefox
Edge
IE
10+
Safari
5.1+
Opera
iOS
Android

入力内容を検証し、検証エラーがある場合にinvalidイベントを発生させます。

HTMLSelectElement.item()

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

インデックス番号からoption要素を返します。element[index]というリテラル構文でも同じ結果を得られます。

HTMLSelectElement.namedItem()

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

名前からoption要素を取得します。

HTMLSelectElement.remove()

Chrome
Firefox
Edge
IE
Safari
Opera
iOS
Android

select要素、または、option要素を削除します。

HTMLSelectElement.reportValidity()

Chrome
40+
Firefox
49+
Edge
×
IE
×
Safari
×
Opera
27+
iOS
×
Android
×

入力内容を検証し、検証エラーがある場合にinvalidイベントを発生させ、さらに、検証エラーメッセージを表示します。

HTMLSelectElement.setCustomValidity()

Chrome
Firefox
Edge
IE
10+
Safari
5.1+
Opera
iOS
Android

カスタム検証エラー状態にします。または、カスタム検証エラー状態を解除します。

定数

Nodeの定数を利用できます。

サポート状況

HTMLSelectElementのサポート状況です。

ブラウザサポート状況ブラウザサポート状況
ChromeSupportedFirefoxSupported
EdgeSupportedInternet ExplorerSupported
SafariSupportedOperaSupported
iOS SafariSupportedAndroidSupported

Chrome

バージョンサポート状況公開時期シェア
57Supported2017年3月頃0.1%
56Supported2017年1月頃12.02%
55Supported2016年12月頃12.36%
54Supported2016年10月頃0.44%
53Supported2016年9月頃0.25%
52Supported2016年7月頃0.27%
51Supported2016年6月頃0.4%
50Supported2016年4月頃0.32%
49Supported2016年3月頃0.47%
48Supported2016年1月頃0.02%
47Supported2015年12月頃0.06%
46Supported2015年10月頃0.56%
45Supported2015年9月頃0.01%
44Supported2015年7月頃0.83%
43Supported2015年5月頃0.05%
42Supported2015年4月頃0.05%
41Supported2015年3月頃0.01%
40Supported2015年1月頃0.59%
39Supported2014年11月頃0.02%
38Supported2014年10月頃0.08%
37Supported2014年8月頃0.01%
36Supported2014年7月頃0.01%
35Supported2014年5月頃0.01%
34Supported2014年4月頃0.14%
33Supported2014年2月頃0.01%
32Supported2014年1月頃0%
31Supported2013年11月頃0.01%
30Supported2013年10月頃0.03%
29Supported2013年8月頃0%
28Supported2013年6月頃0.03%
27Supported2013年5月頃0.01%
26Supported2013年3月頃0%
25Supported2013年2月頃0%
24Supported2013年1月頃0%
23Supported2012年11月頃0%
22Supported2012年9月頃0%
21Supported2012年7月頃0%
20Supported2012年6月頃0%
19Supported2012年5月頃0%
18Supported2012年3月頃0.01%
17Supported2012年2月頃0.01%
16Supported2011年12月頃0%
15Supported2011年10月頃0%
14Supported2011年9月頃0%

Firefox

バージョンサポート状況公開時期シェア
52Supported2017年3月頃0.12%
51Supported2017年1月頃1.7%
50Supported2016年11月頃0.78%
49Supported2016年9月頃0.02%
48Supported2016年8月頃0.07%
47Supported2016年6月頃0.03%
46Supported2016年4月頃0.01%
45Supported2016年3月頃0.1%
44Supported2016年1月頃0.01%
43Supported2015年12月頃0.02%
42Supported2015年11月頃0.01%
41Supported2015年9月頃0%
40Supported2015年8月頃0.02%
39Supported2015年7月頃0.01%
38Supported2015年5月頃0.02%
37Supported2015年3月頃0%
36Supported2015年2月頃0.01%
35Supported2015年1月頃0%
34Supported2014年12月頃0.01%
33Supported2014年10月頃0%
32Supported2014年9月頃0%
31Supported2014年7月頃0.01%
30Supported2014年6月頃0%
29Supported2014年4月頃0%
28Supported2014年3月頃0.01%
27Supported2014年2月頃0%
26Supported2013年12月頃0%
25Supported2013年10月頃0%
24Supported2013年9月頃0%
23Supported2013年8月頃0%
22Supported2013年6月頃0%
21Supported2013年5月頃0%
20Supported2013年4月頃0%
19Supported2013年2月頃0%
18Supported2013年1月頃0%
17Supported2012年11月頃0%
16Supported2012年10月頃0%
15Supported2012年8月頃0%
14Supported2012年7月頃0%
13Supported2012年6月頃0%
12Supported2012年4月頃0%
11Supported2012年3月頃0%
10Supported2012年1月頃0%
9Supported2011年12月頃0%
8Supported2011年11月頃0%
7Supported2011年9月頃0%
6Supported2011年8月頃0%
5Supported2011年6月頃0%
4Supported2011年3月頃0%

Edge

バージョンサポート状況公開時期シェア
14Supported2016年2月頃1.05%
13Supported2015年9月頃0.08%

Internet Explorer

バージョンサポート状況公開時期シェア
11Supported2013年10月頃4.99%
10Supported2012年8月頃0.09%
9Supported2011年3月頃0.18%
8Supported2009年3月頃0.04%
7Supported2006年10月頃0.01%
6Supported2001年8月頃0%

Safari

バージョンサポート状況公開時期シェア
10Supported2016年10月頃0%
9.1Supported2015年9月頃0%
8.0Supported2014年10月頃0%
7.1Supported2013年10月頃0%
6.0Supported2012年7月頃0%
5.1Supported2011年7月頃0%
4Supported2009年6月頃0%

Opera

バージョンサポート状況公開時期シェア
43Supported2017年2月頃0.06%
42Supported2016年12月頃0.09%
41Supported2016年10月頃0.01%
40Supported2016年9月頃0%
39Supported2016年8月頃0%
38Supported2016年6月頃0%
37Supported2016年5月頃0.01%
36Supported2016年3月頃0.01%
35Supported2016年2月頃0%
34Supported2015年12月頃0%
33Supported2015年10月頃0%
32Supported2015年9月頃0%
31Supported2015年8月頃0%
30Supported2015年6月頃0%
29Supported2015年4月頃0%
28Supported2015年3月頃0%
27Supported2015年1月頃0%
26Supported2014年12月頃0%
25Supported2014年10月頃0%
24Supported2014年9月頃0%
23Supported2014年7月頃0%
22Supported2014年6月頃0%
21Supported2014年5月頃0.01%
20Supported2014年3月頃0%
19Supported2014年1月頃0%
18Supported2013年11月頃0%
17Supported2013年10月頃0%
16Supported2013年8月頃0%
15Supported2013年7月頃0%
12Supported2012年6月頃0.02%
11.6Supported2011年12月頃0%
11.1Supported2011年4月頃0%

iOS Safari

バージョンサポート状況公開時期シェア
10.0Supported2016年9月頃39.65%
9.1Supported2015年9月頃5.39%
8.3Supported2014年9月頃1.03%
7.0Supported2013年9月頃0.24%
6.0Supported2012年9月頃0.05%
5.1Supported2011年10月頃0.03%

Android

バージョンサポート状況公開時期シェア
4.4Supported2013年10月頃0%
4.3Supported2013年7月頃0%
4.2Supported2013年3月頃0.03%
4.1Supported2012年12月頃0%
4.0Supported2012年6月頃1.61%
2.3Supported2011年10月頃0%