HTMLTextAreaElement: select-Event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Das select-Ereignis wird ausgelöst, wenn Text ausgewählt wurde.

Syntax

Verwenden Sie den Ereignisnamen in Methoden wie addEventListener() oder setzen Sie eine Ereignis-Handler-Eigenschaft.

js
addEventListener("select", (event) => { })

onselect = (event) => { }

Ereignistyp

Ein generisches Event.

Beispiele

Auswahlprotokollierer

html
<textarea>Try selecting some text in this element.</textarea>
<p id="log"></p>
js
function logSelection(event) {
  const log = document.getElementById("log");
  const selection = event.target.value.substring(
    event.target.selectionStart,
    event.target.selectionEnd,
  );
  log.textContent = `You selected: ${selection}`;
}

const textarea = document.querySelector("textarea");
textarea.addEventListener("select", logSelection);

Gleichwertig mit onselect

Sie können den Ereignis-Handler auch mit der onselect-Eigenschaft einrichten:

js
textarea.onselect = logSelection;

Spezifikationen

Specification
HTML
# event-select
HTML
# handler-onselect

Browser-Kompatibilität

Siehe auch