4

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

thank you. In Chrome, when an anchor tag is clicked with shift+click it opens a browser in new window with address bar, tab bar etc. but is there any way to achieve this behavior by just clicking the anchor tag (without shift) and open the browser in new window with address bar, tab bar? Tried with the above approach you suggested by dispatching a MouseEvent with shiftKey set to true. But it behaved like a normal click and opened the link in the same window. Any inputs on this would be really helpful.
I have a simple test page here codepen.io/atwayne/pen/vYyWezJ . if you click the without shift button it will open within the window while the shift button will open in a new window. looks like exactly what you want
could you create an example that doesn't work?
thank you for the codepen example. have modified the code and it worked fine as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.