メインコンテンツへスキップ

optgroup要素


1.仕様

OPTGROUP要素は、
共通のラベルを持つ
option要素のグループを
表します。

optgroup

オプショングループ (option group)

種類

通常要素

カテゴリ

なし

コンテンツモデル
0個以上のoption要素 オプションでが混在

0個以上のoption要素が配置できます。オプションで、を混在して配置できます。

に分類される要素は、 です。

タグの省略

終了タグは省略できます。

コンテンツ属性
グローバル属性 他
disabled属性
フォームコントロールが無効かどうか
label属性
ユーザーに見えるラベル
DOM インターフェイス
HTMLOptGroupElement
  1. [Exposed=Window]
  2. interface HTMLOptGroupElement : HTMLElement {
  3. [HTMLConstructor] constructor();
  4.  
  5. [CEReactions] attribute boolean disabled;
  6. [CEReactions] attribute DOMString label;
  7. };
HTMLElement
  1. [Exposed=Window]
  2. interface HTMLElement : Element {
  3. [HTMLConstructor] constructor();
  4.  
  5. // metadata attributes
  6. [CEReactions] attribute DOMString title;
  7. [CEReactions] attribute DOMString lang;
  8. [CEReactions] attribute boolean translate;
  9. [CEReactions] attribute DOMString dir;
  10.  
  11. // user interaction
  12. [CEReactions] attribute (boolean or unrestricted double or DOMString)? hidden;
  13. [CEReactions] attribute DOMString inert;
  14. undefined click();
  15. [CEReactions] attribute DOMString accessKey;
  16. readonly attribute DOMString accessKeyLabel;
  17. [CEReactions] attribute boolean draggable;
  18. [CEReactions] attribute boolean spellcheck;
  19. [CEReactions] attribute DOMString writingSuggestions;
  20. [CEReactions] attribute DOMString autocapitalize;
  21.  
  22. [CEReactions] attribute [LegacyNullToEmptyString] DOMString innerText;
  23. [CEReactions] attribute [LegacyNullToEmptyString] DOMString outerText;
  24.  
  25. ElementInternals attachInternals();
  26.  
  27. // The popover API
  28. undefined showPopover();
  29. undefined hidePopover();
  30. boolean togglePopover(optional boolean force);
  31. [CEReactions] attribute DOMString? popover;
  32. };
  33.  
  34. HTMLElement includes GlobalEventHandlers;
  35. HTMLElement includes ElementContentEditable;
  36. HTMLElement includes HTMLOrSVGElement;
Element
  1. [Exposed=Window]
  2. interface Element : Node {
  3. readonly attribute DOMString? namespaceURI;
  4. readonly attribute DOMString? prefix;
  5. readonly attribute DOMString localName;
  6. readonly attribute DOMString tagName;
  7.  
  8. [CEReactions] attribute DOMString id;
  9. [CEReactions] attribute DOMString className;
  10. [SameObject, PutForwards=value] readonly attribute DOMTokenList classList;
  11. [CEReactions, Unscopable] attribute DOMString slot;
  12.  
  13. boolean hasAttributes();
  14. [SameObject] readonly attribute NamedNodeMap attributes;
  15. sequence<DOMString> getAttributeNames();
  16. DOMString? getAttribute(DOMString qualifiedName);
  17. DOMString? getAttributeNS(DOMString? namespace, DOMString localName);
  18. [CEReactions] undefined setAttribute(DOMString qualifiedName, DOMString value);
  19. [CEReactions] undefined setAttributeNS(DOMString? namespace, DOMString qualifiedName, DOMString value);
  20. [CEReactions] undefined removeAttribute(DOMString qualifiedName);
  21. [CEReactions] undefined removeAttributeNS(DOMString? namespace, DOMString localName);
  22. [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
  23. boolean hasAttribute(DOMString qualifiedName);
  24. boolean hasAttributeNS(DOMString? namespace, DOMString localName);
  25.  
  26. Attr? getAttributeNode(DOMString qualifiedName);
  27. Attr? getAttributeNodeNS(DOMString? namespace, DOMString localName);
  28. [CEReactions] Attr? setAttributeNode(Attr attr);
  29. [CEReactions] Attr? setAttributeNodeNS(Attr attr);
  30. [CEReactions] Attr removeAttributeNode(Attr attr);
  31.  
  32. ShadowRoot attachShadow(ShadowRootInit init);
  33. readonly attribute ShadowRoot? shadowRoot;
  34.  
  35. Element? closest(DOMString selectors);
  36. boolean matches(DOMString selectors);
  37. boolean webkitMatchesSelector(DOMString selectors); // legacy alias of .matches
  38.  
  39. HTMLCollection getElementsByTagName(DOMString qualifiedName);
  40. HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
  41. HTMLCollection getElementsByClassName(DOMString classNames);
  42.  
  43. [CEReactions] Element? insertAdjacentElement(DOMString where, Element element); // legacy
  44. undefined insertAdjacentText(DOMString where, DOMString data); // legacy
  45. };
GlobalEventHandlers
  1. interface mixin GlobalEventHandlers {
  2. attribute EventHandler onabort;
  3. attribute EventHandler onauxclick;
  4. attribute EventHandler onbeforeinput;
  5. attribute EventHandler onbeforematch;
  6. attribute EventHandler onbeforetoggle;
  7. attribute EventHandler onblur;
  8. attribute EventHandler oncancel;
  9. attribute EventHandler oncanplay;
  10. attribute EventHandler oncanplaythrough;
  11. attribute EventHandler onchange;
  12. attribute EventHandler onclick;
  13. attribute EventHandler onclose;
  14. attribute EventHandler oncontextlost;
  15. attribute EventHandler oncontextmenu;
  16. attribute EventHandler oncontextrestored;
  17. attribute EventHandler oncopy;
  18. attribute EventHandler oncuechange;
  19. attribute EventHandler oncut;
  20. attribute EventHandler ondblclick;
  21. attribute EventHandler ondrag;
  22. attribute EventHandler ondragend;
  23. attribute EventHandler ondragenter;
  24. attribute EventHandler ondragleave;
  25. attribute EventHandler ondragover;
  26. attribute EventHandler ondragstart;
  27. attribute EventHandler ondrop;
  28. attribute EventHandler ondurationchange;
  29. attribute EventHandler onemptied;
  30. attribute EventHandler onended;
  31. attribute OnErrorEventHandler onerror;
  32. attribute EventHandler onfocus;
  33. attribute EventHandler onformdata;
  34. attribute EventHandler oninput;
  35. attribute EventHandler oninvalid;
  36. attribute EventHandler onkeydown;
  37. attribute EventHandler onkeypress;
  38. attribute EventHandler onkeyup;
  39. attribute EventHandler onload;
  40. attribute EventHandler onloadeddata;
  41. attribute EventHandler onloadedmetadata;
  42. attribute EventHandler onloadstart;
  43. attribute EventHandler onmousedown;
  44. [LegacyLenientThis] attribute EventHandler onmouseenter;
  45. [LegacyLenientThis] attribute EventHandler onmouseleave;
  46. attribute EventHandler onmousemove;
  47. attribute EventHandler onmouseout;
  48. attribute EventHandler onmouseover;
  49. attribute EventHandler onmouseup;
  50. attribute EventHandler onpaste;
  51. attribute EventHandler onpause;
  52. attribute EventHandler onplay;
  53. attribute EventHandler onplaying;
  54. attribute EventHandler onprogress;
  55. attribute EventHandler onratechange;
  56. attribute EventHandler onreset;
  57. attribute EventHandler onresize;
  58. attribute EventHandler onscroll;
  59. attribute EventHandler onscrollend;
  60. attribute EventHandler onsecuritypolicyviolation;
  61. attribute EventHandler onseeked;
  62. attribute EventHandler onseeking;
  63. attribute EventHandler onselect;
  64. attribute EventHandler onslotchange;
  65. attribute EventHandler onstalled;
  66. attribute EventHandler onsubmit;
  67. attribute EventHandler onsuspend;
  68. attribute EventHandler ontimeupdate;
  69. attribute EventHandler ontoggle;
  70. attribute EventHandler onvolumechange;
  71. attribute EventHandler onwaiting;
  72. attribute EventHandler onwebkitanimationend;
  73. attribute EventHandler onwebkitanimationiteration;
  74. attribute EventHandler onwebkitanimationstart;
  75. attribute EventHandler onwebkittransitionend;
  76. attribute EventHandler onwheel;
  77. };
ElementContentEditable
  1. interface mixin ElementContentEditable {
  2. [CEReactions] attribute DOMString contentEditable;
  3. [CEReactions] attribute DOMString enterKeyHint;
  4. readonly attribute boolean isContentEditable;
  5. [CEReactions] attribute DOMString inputMode;
  6. };
HTMLOrSVGElement
  1. interface mixin HTMLOrSVGElement {
  2. [SameObject] readonly attribute DOMStringMap dataset;
  3. attribute DOMString nonce; // intentionally no [CEReactions]
  4.  
  5. [CEReactions] attribute boolean autofocus;
  6. [CEReactions] attribute long tabIndex;
  7. undefined focus(optional FocusOptions options = {});
  8. undefined blur();
  9. };
ARIA in HTML
暗黙的="group" ARIAロール="No" aria-* 属性="GlobalAny"

暗黙的な ARIAセマンティクスは、role=group です。

作成者は、暗黙的な ARIAセマンティクス, または HTML要素のネイティブセマンティクスを上書きしてはいけません。

グローバル aria-* 属性と、許可されたロールに適用される任意の aria-* 属性が記述できます。

レンダリング
特になし
  1. optgroup {
  2. 0: N;
  3. 1: o;
  4. 2: t;
  5. 3: h;
  6. 4: i;
  7. 5: n;
  8. 6: g;
  9. }

CSS を大雑把に読むと、 特に指定はされていません。既定値から、フローレイアウトに配置されたときにインラインレベルのボックス(インラインボックス)を生成すると思われます。 などになります。

説明
optgroup要素は、共通のラベルを持つoption要素のグループを表します。
  1. <!DOCTYPE html>
  2. <html lang="ja">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>optgroup要素の例 - (Example of optgroup element) | 離珠のHTMLでしぃ!</title>
  6. </head>
  7. <body>
  8. <h1>optgroup要素の例</h1>
  9. <form action="../Html/fmreceive.htm">
  10. <select name="nseSelect">
  11. <optgroup label="alphabet">
  12. <option>abcdef</option>
  13. <option>ghijkl</option>
  14. </optgroup>
  15. <optgroup label="number">
  16. <option>123456</option>
  17. <option>789012</option>
  18. </optgroup>
  19. </select>
  20. <button name="nbtSender" value="../Html/exoptgroup.htm">ボタン</button>
  21. <br>受信用の<br>ページへ
  22. </form>
  23. </body>
  24. </html>

このサンプルへのリンクです。
(./exoptgroup.htm)

JSONデータ
JSON形式での、データのイメージです。
  1. {
  2. "optgroup": {
  3. "check": "2024-09-01",
  4. "group": "Form",
  5. "element": "- O (option)* +(%Script) -- option group. (Normal elements.) [None] --",
  6. "attlist": {
  7. "%Global;": "",
  8. "disabled": "(disabled) #IMPLIED -- Whether the form control is disabled --",
  9. "label": "%Text; #IMPLIED -- User-visible label --",
  10. },
  11. "comment": "オプショングループ (option group)",
  12. "point": "共通のラベルを持つoption要素のグループを表します。",
  13. "kind": "Normal",
  14. "categories": "None",
  15. "used": "select",
  16. "contentmodel": "optgroup (option)* +(%Script)",
  17. "tag": [
  18. "-",
  19. "O",
  20. ],
  21. "attributes": {
  22. "disabled": "フォームコントロールが無効かどうか",
  23. "label": "ユーザーに見えるラベル",
  24. },
  25. "dominterface": "HTMLOptGroupElement",
  26. "rendering": "None",
  27. "reference": [
  28. {
  29. "title": "HTML Living Standard. 4.10.9 The optgroup element.",
  30. "href": "https://html.spec.whatwg.org/multipage/form-elements.html#the-optgroup-element",
  31. "heading": "HTML Living Standard. 4.10.9 The optgroup element",
  32. "sub": "Last Updated 27 August 2024",
  33. },
  34. {
  35. "title": "HTML 5.2 W3C Recommendation. 4.10.9 The optgroup element. W3C Recommendation, 14 December 2017.",
  36. "href": "https://www.w3.org/TR/2017/REC-html52-20171214/sec-forms.html#the-optgroup-element",
  37. "heading": "HTML 5.2 W3C 勧告. 4.10.9 The optgroup element",
  38. "sub": "W3C勧告、2017年12月14日",
  39. },
  40. {
  41. "title": "HTML 5.1 W3C Recommendation. 4.10.9 The optgroup element. W3C Recommendation, 1 November 2016.",
  42. "href": "https://www.w3.org/TR/2016/REC-html51-20161101/sec-forms.html#the-optgroup-element",
  43. "heading": "HTML 5.1 W3C 勧告. 4.10.9 The optgroup element",
  44. "sub": "W3C勧告、2016年11月1日",
  45. },
  46. {
  47. "title": "HTML 5 W3C Recommendation. 4.10.9 The optgroup element. W3C Recommendation, 28 October 2014.",
  48. "href": "https://www.w3.org/TR/2014/REC-html5-20141028/forms.html#the-optgroup-element",
  49. "heading": "HTML 5 W3C 勧告. 4.10.9 The optgroup element",
  50. "sub": "W3C勧告、2014年10月28日",
  51. },
  52. {
  53. "title": "HTML 4.01 Specification W3C Recommendation. 17.6 The SELECT, OPTGROUP, and OPTION elements. W3C Recommendation 24 December 1999.",
  54. "href": "https://www.w3.org/TR/html401/interact/forms.html#h-17.6",
  55. "heading": "HTML 4.01 Specification W3C 勧告. 17.6 The SELECT, OPTGROUP, and OPTION elements",
  56. "sub": "W3C勧告1999年12月24日",
  57. },
  58. {
  59. "title": "MDN Web Docs. HTML <optgroup>: The HTML Optgroup element.",
  60. "href": "https://developer.mozilla.org/ja/docs/Web/HTML/Element/optgroup",
  61. "heading": "<optgroup>: HTML 選択肢グループ要素 - HTML: ハイパーテキストマークアップ言語 | MDN",
  62. },
  63. ],
  64. "version": {
  65. "first": "HTML4",
  66. "latest": "HTML Living Standard",
  67. },
  68. }
  69. }

注意: 簡易に描画する為に、各項目の後ろに , を付与しています。JSON の形式では、末尾の項目の後には , は付けません。

最終確認日

2024-09-01


2.使用可能な箇所

select要素に
配置できます。

optgroup要素を使用できる状態(コンテキストContext)は、select要素に配置できます。


3.配置できるコンテンツ

option要素を
配置できます。

optgroupは、(内容コンテンツContent)として、0 以上の option要素 および scriptコンテンツを配置できます。

scriptコンテンツである要素は、script要素, template要素 です。


4.属性

グローバル属性と、
disabled,
label を
設定できます。

optgroupは、グローバル属性disabled属性, label属性を設定できます。

  1. <form action="./fmreceive.htm">
  2. <select name="nseSelect">
  3. <optgroup label="alphabet">
  4. <option>abcdefg</option>
  5. <option>ghijkl</option>
  6. </optgroup>
  7. <optgroup label="number">
  8. <option>123456</option>
  9. <option>789012</option>
  10. </optgroup>

form要素を改めて提示していない限り、この雛型の 3 行目のoptgroup要素の開始タグを置き換えてデモとしています。

button要素valueの値は、作成者の決め事によりその都度送信元が分かる様に書き換えています。nameの値は、作成者の決め事によりnbtSenderで固定です。

disabled属性

  1. <option label="alphabet" disabled>

disabledは、コントロールを非対話型にして、その値が送信されないようにする為に使用されるブール属性です。

label属性

  1. <option label="alphabet">

labelは、要素のラベルを提供します。option要素のラベルは、label コンテンツ属性の値 (存在する場合) であり、その値が空の文字列でない場合は、要素の text IDL 属性の値です。

label コンテンツ属性が指定されている場合、空であってはなりません。


5.Web API

id属性を用いて
getElementById()メソッドで
要素を取得する方法がお勧めです。

optgroup要素を表すプロパティはないので、idを用いて documentgetElementById()メソッドで、要素を取得する方法がお勧めです。この時に取得できるのはHTMLOptGroupElement オブジェクトです。


6.レンダリング

レンダリングについて
スタイルシートでの
仕様での提案は
特にないようです。

optgroup要素は、select要素がレンダリングするリストボックスまたはドロップダウンボックスのリストのオプショングループのヘッダーを提供する事が期待されているという記述があるだけで、optgroup要素自体のレンダリングに関してのスタイルシートでの提案はないようです。

optgroup要素は、要素のlabelを表示することによってレンダリングされることが期待されます。

option要素は、要素のラベルを表示することによってレンダリングされることが期待されます。ラベルは、optgroup要素がある場合はその下にインデントされます。

select のラベルの幅は、最も広い optgroup をレンダリングするために必要な幅と、要素のオプションリストで最も広いoption要素をレンダリングするために必要な幅 (インデントがある場合はそれを含む) のうち、広い方の幅になります。

他の要素を代用して同等のレンダリングをする事を考えたら、リストの1項目という事でdisplay: list-itemが意味的には使えるかもしれません。マーカーを意識する必要があるので、手間は増えますが。
技術的にレンダリングだけを考えたら、ブロックレベルボックスを生成すればよいのでdisplay: blockが使い勝手が良いでしょう。代用する要素をdiv要素とするだけで良いので容易です。


7.気を付ける点

インターフェース

optgroup要素は、select要素での選択可能な選択肢のグループを表す要素な訳で、フォームコントロールの一部を成す為のデータを提示するものかと思います。なので、「ユーザー」, 「作成者」だけでなく「送信先」も意識した作りが必要です。 そのままでは、「送信先」にはoptgroup要素の情報は伝わりません。

「ユーザー」を意識したページの分かり易さや使い易さ、「作成者」を意識したソースコードの書き易さや読み易さ、「送信先」を意識した送信データの使い易さなど、バランスが大変ですがとりあえず意味ある作りを心掛けましょう。


8.付記