Use querySelector() or querySelectorAll()
const element = document.querySelector('.my-class');
const allElements = document.querySelectorAll('.my-class');// Select first paragraph
const firstP = document.querySelector('p');
// Select all buttons
const buttons = document.querySelectorAll('button');
// Work with results
buttons.forEach(btn => {
btn.style.color = 'blue';
});const element = document.querySelector('#myId');
// or
const element2 = document.getElementById('myId');const element = document.querySelector('[data-id="123"]');Avoid using jQuery or getElementsByClassName
// DON'T DO THIS
var elements = document.getElementsByClassName('my-class');
// Returns HTMLCollection, not array✓ Works in all modern browsers (IE8+)