-3

i am currently using <a href="somelink.com"><a/> and on click the href the href and Dom event both are called i need only DOM event to be called href should be only used for opening link in new tab

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • @Rory McCrossan I reopened because OP asks requests that CTRL-Click will still allow for the default behaviour (opening the href in a new tab). – connexo Jun 06 '19 at 13:49
  • 2
    Ok, yeah. Fair point. Now it's just offtopic for a whole other reason :) – Rory McCrossan Jun 06 '19 at 13:50
  • @RoryMcCrossan True, so what to do? Edit the question, include the `event.preventDefault()` part and reduce the question to what I pointed out for re-opening? – connexo Jun 06 '19 at 13:51
  • why do you use an href then ? just use a ```

    ```

    – axel axel Jun 06 '19 at 13:52
  • 1
    @axelaxel How would you CTRL-click a `p` for opening in a new tab then? – connexo Jun 06 '19 at 13:53
  • There's no such thing as an "href event". There's a click event, which occurs when you click on the link created by the anchor tag. If you bind an event listener to that, and open the URL in a new tab/window. – Heretic Monkey Jun 06 '19 at 13:53
  • @connexo I don't see anything specific to Ctrl-click, just that the "DOM event to be called href (sic) should only be used for opening link in new tab". That could mean that just normal clicking on the link should open a new tab (a la the old target="_blank" behavior). Hard to tell though, for sure. – Heretic Monkey Jun 06 '19 at 13:57
  • The question needs more details into what is happening. "the href and dom event both are called". What href event? – Taplar Jun 06 '19 at 14:04

1 Answers1

-1

Please try this solution. I hope it will help you.

    <html>
    <head>
      <meta charset="utf-8" />
      <title>Test</title>
    </head>

    <body>
      <a href="https://stackoverflow.com" id="myLink"> My Link</a>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
      <script>
        $("#myLink").click(function(e) {
          event.preventDefault();
          /** Do your code here */
          console.log("Event Triggered");
        });
      </script>
    </body>
  </html>
Alok Mali
  • 2,821
  • 2
  • 16
  • 32
  • OP hasn't asked for jQuery solutions. On top of that, you missed the relevant *href should be only used for opening link in new tab* part. – connexo Jun 06 '19 at 14:12