Hackforge Academy

Category: javascript

DOM events

Published on 03 Mar 2026

Explanation


querySelector() selects the first HTML element that matches a specified CSS selector. It can select by 1.id (#id), 2.class (.class), 3.tag name 4.attribute.

Code:

document.querySelector('selector');

Explanation


getElementById() selects an HTML element using its unique id attribute. It returns a single element.

Code:

document.getElementById('id');

Explanation

String literals (Template literals)
use backticks (` `) and allow embedding variables using ${}. They support multi-line strings and interpolation.

Code:

const message = `Hello ${name}`;

Explanation


DOM events allow JavaScript to respond to user actions like 1.click, 2.submit 3.mouseover, 4.keypress using 5.addEventListener().

Code:

element.addEventListener('click', 
function() { 
console.log('Clicked'); 
});

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community