Vineet Kapoor
Vineet Kapoor

Reputation: 8289

What is the difference between innertext and innerText in html?

If we inspect any element in browser, there are two properties associated with each of them.

  1. innertext and
  2. innerText

(notice 'T' is caps in second one)

document.getElementById(elementId).innerText = 'sometext';

updates inner-text of the element but when I try to do

document.getElementById(elementId).innertext = 'someOtherText';

nothing happens.

Is there any difference between the two. If yes, what is the difference?

Upvotes: 0

Views: 135

Answers (2)

lehiester
lehiester

Reputation: 900

The innerText property is used by all major browsers, so that should be the one you use.

https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText

Browsers historically have individually added a large number of non-standard features, and if you are seeing an innertext property, it was likely just added by that browser for convenience.

Upvotes: 1

jack
jack

Reputation: 1391

.innertext method does not exist in plain JS.

Hence, doing document.getElementById(elementId).innertext = 'someOtherText'; will create a new property called innertext for your object with the value you provided.

Upvotes: 2

Related Questions