Master Accessing HTML Elements with JavaScript Methods

Accessing HTML elements using JavaScript is the most important and fundamental topic by which we can perform DOM manipulation and other work with JavaScript. I will show simple ways to select elements and then change their text or styles. I have some of the HTML part here. The first is a heading whose id is page-title. The next two lines are paragraphs, and their class is intro. The third is a button whose id is my-button.
<h1 id="page-title">My Website</h1>

<p class="intro">This is the first intro paragraph.</p>
<p class="intro">This is the second intro paragraph.</p>

<button id="my-button">Press Me</button>
  Master Accessing HTML Elements with JavaScript Methods  1  

JavaScript DOM Selection by ID with getElementById

  Master Accessing HTML Elements with JavaScript Methods  3   The first method to access an element is by using the document object. The document represents the whole browser page. Then you use the getElementById method to get an element by its id.   Master Accessing HTML Elements with JavaScript Methods  2   Select the element by id.   Master Accessing HTML Elements with JavaScript Methods  4   Store it inside a variable.   Master Accessing HTML Elements with JavaScript Methods  5   Change its inner text.   Master Accessing HTML Elements with JavaScript Methods  6  
const title = document.getElementById('page-title');

// Perform an operation on it
title.innerText = 'Welcome to JS';
  Master Accessing HTML Elements with JavaScript Methods  7   Earlier it was My Website and now it is Welcome to JS. We can simply change the HTML part with the help of JavaScript by performing DOM manipulation and using document.getElementById.   Master Accessing HTML Elements with JavaScript Methods  8  

JavaScript DOM Selection by Class with getElementsByClassName

  Master Accessing HTML Elements with JavaScript Methods  9   The second method is getting elements with the help of class using getElementsByClassName. For selecting elements with a class, this returns a live HTMLCollection, so we typically loop over it.   Master Accessing HTML Elements with JavaScript Methods  13   Select all elements with the class intro.   Master Accessing HTML Elements with JavaScript Methods  10   Store them in a variable.   Master Accessing HTML Elements with JavaScript Methods  12   Loop through the collection up to its length.   Master Accessing HTML Elements with JavaScript Methods  11   Change the style for each indexed element.   Master Accessing HTML Elements with JavaScript Methods  14  
const introParagraphs = document.getElementsByClassName('intro');

for (let i = 0; i < introParagraphs.length; i++) {
  introParagraphs[i].style.color = 'blue';
}
  Master Accessing HTML Elements with JavaScript Methods  15   It will select both paragraphs, because both have the class intro. You can apply conditions too, like styling odd or even indexes differently.   Master Accessing HTML Elements with JavaScript Methods  16  

JavaScript DOM Selection with querySelector

  Master Accessing HTML Elements with JavaScript Methods  17   querySelector lets you select with CSS selectors. You can select by id or class using the same method.   Master Accessing HTML Elements with JavaScript Methods  18   Use a hash for an id.   Master Accessing HTML Elements with JavaScript Methods  19   Use a dot for a class.   Master Accessing HTML Elements with JavaScript Methods  20   Since the button has an id of my-button, select it with #my-button.   Master Accessing HTML Elements with JavaScript Methods  21  
const button = document.querySelector('#my-button');
// Successfully selected this particular button
  Master Accessing HTML Elements with JavaScript Methods  22   This gives you the first matching element for the selector you provide.   Master Accessing HTML Elements with JavaScript Methods  23  

JavaScript DOM Selection with querySelectorAll

  Master Accessing HTML Elements with JavaScript Methods  24   querySelectorAll selects all elements that match the CSS selector and returns a static NodeList, which supports forEach.   Master Accessing HTML Elements with JavaScript Methods  26   Select all elements with the class intro.   Master Accessing HTML Elements with JavaScript Methods  25   Loop each selected element.   Master Accessing HTML Elements with JavaScript Methods  27   Change the background color. Use backgroundColor, not background color.   Master Accessing HTML Elements with JavaScript Methods  29  
const allIntro = document.querySelectorAll('.intro');

allIntro.forEach((para) => {
  para.style.backgroundColor = 'red';
});
  Master Accessing HTML Elements with JavaScript Methods  28   You can see both paragraphs now have blue text and a red background, showing how we changed the CSS with JavaScript and DOM manipulation.   Master Accessing HTML Elements with JavaScript Methods  30  

Complete Script Example

  Master Accessing HTML Elements with JavaScript Methods  32  
<h1 id="page-title">My Website</h1>

<p class="intro">This is the first intro paragraph.</p>
<p class="intro">This is the second intro paragraph.</p>

<button id="my-button">Press Me</button>

<script>
// By id
const title = document.getElementById('page-title');
title.innerText = 'Welcome to JS';

// By class name
const introParagraphs = document.getElementsByClassName('intro');
for (let i = 0; i < introParagraphs.length; i++) {
  introParagraphs[i].style.color = 'blue';
}

// By CSS selector
const button = document.querySelector('#my-button');

// All by CSS selector
const allIntro = document.querySelectorAll('.intro');
allIntro.forEach((para) => {
  para.style.backgroundColor = 'red';
});
</script>
  Master Accessing HTML Elements with JavaScript Methods  31  

Final Thoughts

We learned how to select HTML elements with JavaScript using getElementById, getElementsByClassName, querySelector, and querySelectorAll, and how to apply DOM manipulation such as changing text and styles. These methods cover selecting by id, by class, and by flexible CSS selectors for single or multiple elements.

Leave a Comment