[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/HTMLInputElement/selectionEnd [Back]  [Original]

HTMLInputElement: selectionEnd property - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

HTMLInputElement: selectionEnd property

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

HTMLInputElement selectionEnd . .

: WHATWG forms spec , selectionEnd text, search, URL, tel, password . , selectionEnd . , selectionEnd null .

selectionEnd selectionStart , selectionEnd .

In this article

.

HTML

html
<!--     selectionEnd  -->
<label for="color">type=color selectionStart </label>
<input id="color" type="color" />

<!--    selectionEnd  -->
<fieldset>
  <legend>type=text selectionEnd </legend>
  <label for="pin">Input PIN</label>
  <input type="text" id="pin" value="impossible PIN: 102-12-145" />
  <button id="pin-btn" type="button">PIN correction</button>
</fieldset>

JavaScript

js
const colorEnd = document.getElementById("color");
const text = document.querySelector("#pin");
const pinBtn = document.querySelector("#pin-btn");
const validPinChecker = /[^\d{3}-\d{2}-\d{3}]/g;
const selectionEnd = text.value.length;
const selectedText = text.value.substring(text.selectionStart, selectionEnd);

pinBtn.addEventListener("click", () => {
  const correctedText = selectedText.replace(validPinChecker, "");
  text.value = correctedText;
});

//       
console.log(colorEnd.selectionEnd); //  : null

Specification
HTML
# dom-textarea/input-selectionend


Web Proxy Viewer  |  New URL  |  Original Page