Is there a way to intercept enter key on a link without using Bookmarklet (href="javascript:..."
)?
I was trying this:
addEventListener('keypress', (e) => {
console.log(e);
if (e.charCode == 13) {
alert('x');
}
});
<a href="#">this is link</a>
If you press the tab and enter the code is not triggered. Only when the link is not in focus the alert will be triggered. Is there a way to trigger JavaScript on press enter over a link? Or is href
the only way? There should be a way to use JavaScript and not have to put JavaScript into every link on the page.
And if you ask why I need this, it's because of this question: how to open a file upload dialog box when a hyperlink is clicked, I want to update my answer and see if I can trigger click on input[type="file"]
when you press enter.