Question 1: Selecting Elements
Which method is used to select the first element in the document with the ID 'myElement'?
- document.getElementByClass('myElement')
- document.querySelector('#myElement')
- document.getElementById('myElement')
- document.querySelectAll('#myElement')
- document.getElementByName('myElement')
Question 2: Adding Event Listeners
Which method is the correct way to attach an event listener to a button with the ID 'myButton' to listen for a click?
- myButton.attachEvent('onclick', myFunction)
- document.getElementById('myButton').addEventListener('click', myFunction)
- myButton.on('click', myFunction)
- document.getElementById('myButton').onClick = myFunction
- addEventListener('click', myFunction, 'myButton')
Question 3: Changing Element Content
How do you change the text content of an HTML element with the ID 'myParagraph' to 'Hello World!' using JavaScript?
- document.getElementById('myParagraph').innerHTML = 'Hello World!';
- document.getElementById('myParagraph').textContent = 'Hello World!';
- document.getElementById('myParagraph').innerText = 'Hello World!';
- myParagraph.value = 'Hello World!'
- All of the above are correct
Question 4: Preventing Default Behavior
Which method is used to prevent the default behavior of an HTML form submission?
- event.preventDefault()
- event.stopPropagation()
- event.stopImmediatePropagation()
- return false;
- stopDefault()
Question 5: Event Bubbling
What is event bubbling in the context of DOM event handling?
- The process of an event handler being executed multiple times on the same element.
- The process of an event propagating from the target element up the DOM tree to its parent elements.
- The process of preventing an event from reaching its target element.
- A technique for capturing events before they reach their target.
- A method for cloning events and triggering them on other elements.
Question 6: Creating New Elements
Which method is used to create a new HTML element in JavaScript?
- document.createElement()
- new Element()
- document.newElement()
- createElement()
- document.addElement()
Question 7: Removing Event Listeners
What method removes an event listener that was previously attached to an element?
- removeEventListener()
- detachEvent()
- off()
- deleteEventListener()
- clearEventListener()
Question 8: Event Delegation
What is the primary benefit of using event delegation?
- It simplifies event handling for dynamic content.
- It improves performance by reducing the number of event listeners.
- It allows you to capture events before they reach their target.
- It automatically prevents default browser behavior.
- Both A and B
Question 9: Target vs. CurrentTarget
In an event handler, what is the difference between `event.target` and `event.currentTarget`?
- `event.target` refers to the element that triggered the event, while `event.currentTarget` refers to the element the event listener is attached to.
- `event.target` refers to the element the event listener is attached to, while `event.currentTarget` refers to the element that triggered the event.
- There is no difference; they both refer to the same element.
- `event.target` refers to the parent element, while `event.currentTarget` refers to the child element.
- `event.target` is only available in Internet Explorer, while `event.currentTarget` is standard.
Question 10: Setting Attributes
How do you set the 'src' attribute of an image element with the ID 'myImage' to 'image.jpg'?
- document.getElementById('myImage').src = 'image.jpg';
- document.getElementById('myImage').setAttribute('src', 'image.jpg');
- document.getElementById('myImage').source = 'image.jpg';
- document.getElementById('myImage').imageSource = 'image.jpg';
- Both A and B are correct