How do you manipulate the DOM and handle events in JavaScript?
Suggested answer
DOM selection: document.querySelector('.my-class') (first match), document.querySelectorAll('div') (NodeList). Manipulation: element.textContent = 'text';, element.innerHTML = '<span>...</span>';, element.setAttribute('class', 'active');, element.classList.add('active');. Event listeners: element.addEventListener('click', handler, useCapture); Remove: element.removeEventListener('click', handler);. Event bubbling vs capturing: Bubbling (default, useCapture=false): event propagates from target up to document. Capturing (useCapture=true): event propagates from document down to target first. stopPropagation(): Stops further propagation. stopImmediatePropagation(): Also prevents other listeners on the same element. preventDefault(): Prevents the browser's default action (e.g., form submission, link navigation). In LWC, use this.template.querySelector() for shadow DOM access.
Practice content for interview preparation; not an official vendor answer. Verify details against current product documentation.
Community comments (0)
No comments yet.
Sign in or create a free account to add a comment. Comments are moderated before they appear.