Events & DOM Manipulation

Learn how to interact with HTML elements and respond to user actions using JavaScript.

Present State

JavaScript allows dynamic interaction with web pages through the Document Object Model (DOM). Using events and DOM manipulation, developers can change page content, style, and structure in response to user actions such as clicks, keypresses, and form submissions, enabling interactive and responsive web applications.

Code Example

Example demonstrating event handling and DOM manipulation:

JavaScript Event & DOM Manipulation Example

// Select button and paragraph elements
const button = document.getElementById('myButton');
const paragraph = document.getElementById('myParagraph');

// Add a click event listener to the button
button.addEventListener('click', () => {
  // Change the paragraph text and style on button click
  paragraph.textContent = "You clicked the button!";
  paragraph.style.color = 'blue';
});
          

This code demonstrates:

  • Selecting HTML elements via getElementById.
  • Adding event listeners to respond to user actions.
  • Changing DOM content dynamically.
  • Modifying CSS styles programmatically.

Description

DOM (Document Object Model): The DOM is a programming interface for HTML documents. It represents the page so that programs can change the document structure, style, and content.

Events: Events are actions or occurrences that happen in the system, such as user interactions like clicks, key presses, or page loads. JavaScript can listen to these events and execute code in response.

Event Listeners: Functions that are called when an event occurs on a target element. They can be attached using methods like addEventListener.

DOM Manipulation: Involves changing the elements, attributes, and styles of the HTML document dynamically using JavaScript.

  • Selecting elements with methods like getElementById, querySelector.
  • Modifying content using textContent, innerHTML.
  • Changing styles via the style property.
  • Creating, appending, or removing elements dynamically.

Applications

  • Interactive form validations and user feedback.
  • Dynamic UI updates without page reloads.
  • Building responsive navigation menus and modal dialogs.
  • Games and animations that respond to user inputs.
  • Single Page Applications (SPAs) that manipulate DOM to show/hide content.

Multiple Choice Questions

Q1: Which method is used to attach an event listener to an element?

  • A. addEvent()
  • B. addListener()
  • C. addEventListener()
  • D. attachEvent()

Answer: C. addEventListener()

Q2: What does document.getElementById('id') do?

  • A. Selects all elements with the given tag name.
  • B. Selects the first element with the specified class.
  • C. Selects the element with the specified ID.
  • D. Creates a new element with the given ID.

Answer: C. Selects the element with the specified ID.

Q3: Which property changes the text content of an HTML element?

  • A. innerText
  • B. textContent
  • C. innerHTML
  • D. All of the above

Answer: D. All of the above

Q4: How do you change the color style of a paragraph with id "para"?

  • A. document.getElementById('para').style.color = 'red';
  • B. document.getElementById('para').color = 'red';
  • C. document.getElementById('para').setColor('red');
  • D. document.getElementById('para').textColor = 'red';

Answer: A. document.getElementById('para').style.color = 'red';