With the event api, it is possible to create a synthetic MouseEvent and have it dispatched programmatically by a DOM element.
By adding an event listener to the document, the event dispatched by the child element can be captured during the bubble phase.
ie:
const element = document.createElement('div')
element.style.background = '#ff0000'
element.style.width = '200px'
element.style.height = '200px'
document.body.appendChild(element)
document.addEventListener('click', event => {
console.log('Clicked on child element')
console.log(event)
})
const clickEvent = new MouseEvent('click', {
bubbles: true,
cancellable: true,
clientX: 32,
clientY: 32,
})
element.dispatchEvent(clickEvent)
Is it possible to programmatically dispatch a MouseEvent or PointerEvent from a parent element and have the event go through capture phase and trigger the event listener of the child element under the coordinate of the mouse event?