What is the purpose of an event in JavaScript?
A). A. To add styles to HTML elements
B). B. To handle user interactions and browser actions
C). C. To define function names
D). D. To create animations on web pages
How can you prevent the default behavior of an event in JavaScript?
A). A. By using the event.preventDefault() method inside the event handler function
B). B. By using the event.stopPropogation() method inside the event handler function
C). C. By using the event.stopPropagation() method inside the event handler function
D). D. By using the event.defaultPrevented property inside the event handler function
How can you attach multiple event listeners to the same HTML element in JavaScript without overwriting existing ones?
A). A. By using the attachEvent() method
B). B. By using the addEventListener() method with the replace parameter
C). C. By assigning multiple functions directly to the element's event attributes (e.g., onclick, onmouseover)
D). D. By using the addEventListener() method multiple times for the same event
Consider the following HTML code: Which JavaScript code adds a click event listener to the button?
A). A. document.getElementById('myButton').onclick = function() { alert('Button clicked'); };
B). B. document.getElementById('myButton').addEventListener('click', function() { alert('Button clicked'); });
C). C. document.getElementById('myButton').click(function() { alert('Button clicked'); });
D). D. document.getElementById('myButton').on('click', function() { alert('Button clicked'); });
Which event is triggered when a user clicks on an HTML element?
A). A. onhover
B). B. onsubmit
C). C. onload
D). D. onclick
Which event is triggered when a web page finishes loading?
A). A. onload
B). B. onsubmit
C). C. onmouseover
D). D. onclick
Which statement accurately describes JavaScript event bubbling?
A). A. It refers to the downward propagation of events from parent elements to child elements.
B). B. It refers to the upward propagation of events from child elements to parent elements.
C). C. It refers to the simultaneous firing of multiple events on the same element.
D). D. It refers to the automatic removal of event listeners after an event is triggered.
What is the purpose of the onclick attribute in HTML elements?
A). A. To define styles for HTML elements
B). B. To attach event listeners for mouse clicks
C). C. To insert JavaScript code directly into HTML
D). D. To create animations for HTML elements
In JavaScript, what is the purpose of the addEventListener() method?
A). A. To create inline event handlers
B). B. To attach event listeners to HTML elements
C). C. To remove event listeners from HTML elements
D). D. To bind event handlers to parent elements
Consider the following JavaScript code: let greet = function(name) { return 'Hello, ' + name + '!'; }; What type of function is greet in this code?
A). A. Named function
B). B. Anonymous function
C). C. Arrow function
D). D. Generator function