-1

Please let me know how to use getElementById, innerText, innerHTML in JavaScript. As I am new to this, it's a bit confusing.

Gemini Girl
  • 39
  • 1
  • 10
  • When you need them, makes sense? You use JavaScript to bring your page to life, so you're most likely going to use `getElementById` frequently – Adam Azad Dec 12 '16 at 17:46
  • `getElementById` when you want to get an element by ID. Which is pretty often. The others are generally better avoided. :P At least til you know when and why to ignore that advice. – cHao Dec 12 '16 at 17:47
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript – The Alpha Dec 12 '16 at 17:47
  • Use them whenever your tutorial suggests to. As you may already have found, `innerText` is an obsolete property that you should not be using. –  Dec 12 '16 at 17:52

1 Answers1

2

The getElementById():

The getElementById() method returns the element that has the ID attribute with the specified value.

This method is one of the most common methods in the HTML DOM, and is used almost every time you want to manipulate, or get info from, an element on your document.

Returns null if no elements with the specified ID exists.

An ID should be unique within a page. However, if more than one element with the specified ID exists, the getElementById() method returns the first element in the source code.

We use getElementById() to select unique DOM nodes, IDs can't be written more than 1 time. In a successful selection you'll have an object in hand, corresponding to the element you needed to get.

The innerText:

You can use this kind of property to retrieve and set the content of the tag as plain text. You can use it in elements like <a>, <label>, <span>, <p>, etc.

The innerHTML:

innerHTML property sets or returns the HTML content (inner HTML) of an element.

The innerHTML lets you work with HTML rich text and doesn't automatically encode and decode text.

Some contents by @alejo802.

You can see a bit more in: Difference between innerText and innerHTML in javascript

And here: http://www.w3schools.com/jsref/met_document_getelementbyid.asp

Hope it helps!

Community
  • 1
  • 1
RPichioli
  • 3,245
  • 2
  • 25
  • 29