JavaScript Events & Event Handling Flashcards

(30 cards)

1
Q

What is an event in JavaScript?

A

An action that occurs in the browser, such as a click, key press, or form submission.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an event listener?

A

A function that waits for and responds to specific events on an element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you add an event listener to an element?

A

Use .addEventListener('event', callback). Example: button.addEventListener('click', () => {...});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you remove an event listener?

A

Use .removeEventListener('event', callback). Example: button.removeEventListener('click', handleClick);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the event object?

A

An object automatically passed to event handlers that contains details about the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you access the element that triggered the event?

A

Use event.target. Example: console.log(event.target);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you prevent a form from submitting?

A

Use event.preventDefault(). Example: form.addEventListener('submit', e => e.preventDefault());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you stop an event from bubbling up to parent elements?

A

Use event.stopPropagation();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is event bubbling?

A

When an event starts on the target element and moves up through its ancestors in the DOM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is event capturing?

A

The opposite of bubbling; the event moves from the top element down to the target.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the default event flow in JavaScript?

A

Bubbling (from the target element upward).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you handle a click event on a button?

A

Use button.addEventListener('click', () => {...});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you handle keyboard input?

A

Use document.addEventListener('keydown', e => {...}); or keyup. Example: if(e.key === 'Enter') {...}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you handle input changes in a text field?

A

Use input.addEventListener('input', e => {...});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you handle a form submission?

A

Use form.addEventListener('submit', e => { e.preventDefault(); ... });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you listen for when the mouse enters or leaves an element?

A

Use mouseenter and mouseleave events.

17
Q

How do you detect when a user hovers over an element?

A

Use the mouseover event.

18
Q

How do you detect when a user scrolls?

A

Use window.addEventListener('scroll', () => {...});

19
Q

How do you listen for window resizing?

A

Use window.addEventListener('resize', () => {...});

20
Q

How do you check which key was pressed?

A

Use event.key inside a keyboard event handler. Example: if (event.key === 'Enter') {...}

21
Q

How do you listen for when the page finishes loading?

A

Use window.addEventListener('load', () => {...});

22
Q

How do you listen for when the DOM is ready (before full page load)?

A

Use document.addEventListener('DOMContentLoaded', () => {...});

23
Q

How do you attach one event listener to multiple elements?

A

Use querySelectorAll() and a loop. Example: buttons.forEach(btn => btn.addEventListener('click', handler));

24
Q

How do you toggle a class when a button is clicked?

A

Use .classList.toggle('className') inside the click handler.

25
How do you show an alert when a button is clicked?
Use `button.addEventListener('click', () => alert('Button clicked!'));`
26
What is a simple example of event delegation?
Listen on a parent element and use `event.target` to detect which child was clicked.
27
How can you log all user clicks on the page?
Use `document.addEventListener('click', e => console.log(e.target));`
28
What does `event.type` return?
It returns the type of event, such as `'click'`, `'keydown'`, or `'submit'`.
29
Can you attach multiple event listeners to one element?
Yes, an element can have multiple event listeners for the same or different events.
30
How do you trigger an event manually?
Use `.click()` or `dispatchEvent()`. Example: `button.click();`