Why does the error occur?
The most common mistake people make is the d character error. Correctly write
‘d’, if you write
‘D’ you will get the following error. And the second error occurs when the get method is called on a DOM element instead of a document. If you write it wrong, you will get an error below:
Html code sample:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <div id="text">Hello</div> <script src="index.js"></script> </body> </html>
//Wrong character D let text = document.getElementByID('text'); text.textContent = "Hello, Welcome to LearnshareIT"; //Wrong because the getElementById is called on an element text.getElementById('text').textContent = "Hello, Welcome to LearnshareIT";
Uncaught TypeError: document.getElementByID is not a function
The getElementById method will return the Element Object if an element with the specified id is found and will return null if no matching element is found.
- id: the id attribute value of the element to get.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <p id ="p">This is a paragraph</p> <script> console.log(document.getElementById("p")); </script> </body> </html>
<p id="p">This is a paragraph</p>
Spell check and use
To fix that error, we must use Id instead of ID and call the method on the document object.
let text = document.getElementById('text'); text.textContent = "Hello, Welcome to LearnshareIT"; console.log(text);
<div id="text">Hello, Welcome to LearnshareIT</div>
The error is fixed, and the method works properly if you use it properly.
Name of the university: HVNH BA
Major: htttql MIS