HTMLElement: autocorrect property

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("HTML DOM")}}

The **autocorrect** property of the {{domxref("HTMLElement")}} interface controls whether or not autocorrection of editable text is enabled for spelling and/or punctuation errors.

The specific autocorrection behavior, including which words are substituted, depends on the user agent and the services provided by the underlying device. For example, on macOS a user agent might rely on registered replacement text and punctuation. Other devices and browsers may use a different approach.

The property reflects the value of the [autocorrect](/en-US/docs/Web/HTML/Reference/Global_attributes/autocorrect) HTML global attribute.

Value

true if auto-correction is enabled for the element, and false otherwise.

Examples

Enable and disable autocorrection

This example shows how you can enable and disable autocorrection.

HTML

The HTML markup defines a toggle button and an {{htmlelement("input")}} element of [type="search"](/en-US/docs/Web/HTML/Reference/Elements/input/search). Note that if auto-correction is supported, it will be enabled by default.

html
<button id="toggleAutocorrect">Unknown</button>
<input type="search" id="searchinput" />
html hidden
css hidden
js hidden

JavaScript

The code first checks whether the autocorrect is supported by checking if it is present on the HTMLElement prototype. If it is present, a click handler is added to allow you to toggle the value. If it is not present, the UI hides the interactive elements and logs that autocorrect is not supported.

js
const toggleButton = document.querySelector("button");
const searchInput = document.querySelector("#searchinput");

function setButtonText() {
  toggleButton.textContent = searchInput.autocorrect ? "Enabled" : "Disabled";
  log(`autocorrect: ${searchInput.autocorrect}`);
}

if (`autocorrect` in HTMLElement.prototype) {
  setButtonText();

  toggleButton.addEventListener("click", (e) => {
    searchInput.autocorrect = !searchInput.autocorrect;
    setButtonText();
  });
} else {
  toggleButton.hidden = true;
  searchInput.hidden = true;
  log("autocorrect not supported");
}

Result

<!-- cSpell:ignore Carot -->

Activate the button to toggle the autocorrect value. Enter invalid text into the text box, such as "Carot". When the autocorrect is enabled, and if the implementation has the appropriate substitute word "carrot", the text should automatically be corrected.

{{EmbedLiveSample("Enable and disable autocorrection", "100%", "200")}}

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • [autocapitalize](/en-US/docs/Web/HTML/Reference/Global_attributes/autocapitalize) HTML global attribute