There are 2 anchor elements as shown below.
JavaScript
x
3
1
<a (click)="popupIconClick()">Click Here</a>
2
<a #newWindow (click)="openInNewWindow($event)">New Window</a>
3
when first element is clicked, it invokes popupIconClick()
function in my .ts file. This finds the #newWindow
element and should invoke the click function for this element but the click event should behave as if it was performed with shift key pressed.
JavaScript
1
5
1
popupIconClick() {
2
const newWindowElement = document.querySelector(`#newWindow`);
3
//should invoke shift+click for newWindowElement
4
}
5
Is this possible?
Advertisement
Answer
Dispatch a MouseEvent
with shiftKey
set to true
JavaScript
1
2
1
newWindowElement.dispatchEvent(new MouseEvent("click", { shiftKey: true}));
2
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/shiftKey