← Back to excelora.in Free courses by the Excelora team · Learn by doing
Hire Excelora

Lesson 4 of 4 · 15 min read

JavaScript: making pages respond

JavaScript listens for events (clicks, typing, scrolling) and changes the page in response:

const btn = document.querySelector('#menuBtn');
btn.addEventListener('click', () => {
  document.body.classList.toggle('menu-open');
});

Golden rules for safe JavaScript:

  • Insert user text with textContent, never innerHTML — this blocks XSS attacks.
  • Never put API keys in JavaScript — anyone can read them. Keep secrets on the server (PHP).
Next step: take the PHP & MySQL course to build the server side.

⚡ Quick check

Safest way to show text a user typed?