Lecture
Selecting HTML Elements - 1
getElementById
document.getElementById() is a method used to select an element with a specific id attribute in HTML.
Code Example:
HTML: p element with intro id
<!-- HTML p element with an id --> <p id="intro">Hello</p>
JavaScript: Selecting element with getElementById
// Using JavaScript to select the element and change its content const introElement = document.getElementById('intro'); introElement.textContent = 'Nice to meet you';
getElementsByTagName
document.getElementsByTagName() is a method used to select all elements with a specific tag in HTML.
Code Example:
HTML: Multiple p elements
<!-- Several <p> elements in HTML --> <p>This is the first paragraph.</p> <p>This is the second paragraph.</p> <p>This is the third paragraph.</p>
JavaScript: Selecting elements with getElementsByTagName
// Using JavaScript to select all elements with <p> tags const paragraphs = document.getElementsByTagName('p'); for (let para of paragraphs) { para.style.color = ''; }
Lessons in this chapter ยท How does JavaScript work in the browser?
- 1. How Do Browsers Work?
- 2. DOM (Document Object Model)
- 3. Browser Window Object
- 4. Window - window.screen
- 5. Window - window.location
- 6. Methods to Select HTML Elements - 1
- 7. How to Select HTML Elements - 2
- 8. Creating and Setting Attributes of HTML Elements
- 9. Events
- 10. Advanced Event Handling
- 11. Multiple-choice quiz
- 12. JSON(JavaScript Object Notation)
- 13. Promise
- 14. Async/Await Handling Asynchronous Processes
- 15. Cookies
- 16. Multiple-choice quiz
- 17. JavaScript Summary
Quiz
0 / 1
What is the most appropriate answer to fill in the blank?
The method used to select an HTML element by its specific id is .
getElementById
getElementsByTagName
querySelector
getElementsByClassName
Lecture
AI Tutor
Design
Upload
Notes
Favorites
Help