Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to trigger shift + left mouse click in Angular

There are 2 anchor elements as shown below.

<a (click)="popupIconClick()">Click Here</a>
<a #newWindow (click)="openInNewWindow($event)">New Window</a>

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.

popupIconClick() {
  const newWindowElement = document.querySelector(`#newWindow`);
  //should invoke shift+click for newWindowElement
}

Is this possible?

like image 704
apdp Avatar asked Nov 07 '25 08:11

apdp


1 Answers

Dispatch a MouseEvent with shiftKey set to true

newWindowElement.dispatchEvent(new MouseEvent("click", { shiftKey: true}));

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/shiftKey

like image 138
lastr2d2 Avatar answered Nov 09 '25 21:11

lastr2d2



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!