aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/api/htmlinputelement/selectionchange_event/index.md
blob: 5a3c85f7b14e940570302ba8866c855a2f551210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
---
title: 'HTMLInputElement: selectionchange イベント'
slug: Web/API/HTMLInputElement/selectionchange_event
tags:
  - API
  - Event
  - リファレンス
  - Selection
  - 選択 API
  - selectionchange
  - 実験的
browser-compat: api.HTMLInputElement.selectionchange_event
translation_of: Web/API/HTMLInputElement/selectionchange_event
---
{{APIRef}}{{SeeCompatTable}}

**`selectionchange`** は[選択 API](/ja/docs/Web/API/Selection) のイベントで、 {{HTMLElement("input")}} 要素の中でテキストの選択状態が変化したときに発行されます。
これは、文字単位の選択範囲位が変化した場合も、キャレットが移動したときも含みます。

<table class="properties">
  <tbody>
    <tr>
      <th>バブリング</th>
      <td>はい</td>
    </tr>
    <tr>
      <th>キャンセル</th>
      <td>不可</td>
    </tr>
    <tr>
      <th>インターフェイス</th>
      <td>{{domxref("Event")}}</td>
    </tr>
    <tr>
      <th>イベントハンドラープロパティ</th>
      <td>
        {{domxref("GlobalEventHandlers.onselectionchange", "onselectionchange")}}
      </td>
    </tr>
  </tbody>
</table>

このイベントは通常 {{HTMLElement("input")}} 上にイベントリスナーを追加し、ハンドラー関数内で {{domxref("HTMLInputElement")}} の `selectionStart`, `selectionEnd`, `selectionDirection` の各プロパティを読み取ることで処理します。

また、グローバルな {{domxref("GlobalEventHandlers.onselectionchange", "onselectionchange")}} イベントハンドラーにリスナーを追加し、ハンドラー関数内で {{domxref("Document.getSelection()")}} を使って{{domxref("Selection", "選択状態", "", 1)}}を得ることもできます。しかし、これはテキストの選択範囲の変更を取得するのにはあまり便利ではありません。

## 例

以下の例は、 {{HTMLElement("input")}} 要素の中にあるテキストの選択状態を取得する方法を示しています。

### HTML

```html
<div>Enter and select text here:<br><input id="mytext" rows="2" cols="20"></div>
<div>selectionStart: <span id="start"></span></div>
<div>selectionEnd: <span id="end"></span></div>
<div>selectionDirection: <span id="direction"></span></div>
```

### JavaScript

```js
const myinput = document.getElementById("mytext");

myinput.addEventListener("selectionchange", () => {
  document.getElementById("start").textContent = myinput.selectionStart;
  document.getElementById("end").textContent = myinput.selectionEnd;
  document.getElementById("direction").textContent = myinput.selectionDirection;
});
```

### 結果

{{EmbedLiveSample("Examples")}}

## 仕様書

{{Specifications}}

## ブラウザーの互換性

{{Compat}}

## 関連情報

- {{domxref("GlobalEventHandlers.onselectionchange")}}