Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I would like to add an addEventListener on click for a specific hyperlink in a page. Here's what I got so far.

What it does is, every time a user clicks on a page, it sends me that log. I don't want that, I need to receive the log whenever the user clicks on that specific link. In this case, the hyperlink is "link".

Looking forward for any inputs you can share. Thank you very much.

What I have tried:

Part of the code:

let link = document.createElement("a");

document.querySelectorAll('[class^="tabs-content"]').forEach(link => {
link.addEventListener('click', (e) => {
log('Test');
});
});
Posted
Updated 15-Jun-23 4:30am
v8
Comments
Karthik_Mahalingam 23-May-23 23:22pm    
what is the issue?

1 solution

This should work:
JavaScript
// Get all anchor elements with a specific class name
var anchors = document.getElementsByClassName("awsui_tabs-content");

// clicked counter
var count = 0;

// Add click event listener to each anchor
Array.from(anchors).forEach(function (anchor) {
    anchor.addEventListener("click", function (event) {
        console.log("Clicked! count: " + ++count);
        // Add your custom logic here
    });
});

UPDATE
This was my test:
HTML
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
        <div id="app">
            <a href="#" class="my_anchor">anchor 1</a>
            <a href="#" class="my_anchor">anchor 2</a>
            <a href="#" class="my_anchor">anchor 3</a>
            <a href="#" class="my_anchor">anchor 4</a>
            <a href="#" class="my_anchor">anchor 5</a>
            <a href="#" class="my_anchor">anchor 6</a>
            <a href="#" class="my_anchor">anchor 7</a>
            <a href="#" class="my_anchor">anchor 8</a>
        </div>
    </body>
    <script>
// Get all anchor elements with a specific class name
var anchors = document.getElementsByClassName("my_anchor");
var count = 0;
// Add click event listener to each anchor
Array.from(anchors).forEach(function (anchor) {
    anchor.addEventListener("click", function (obj) {
        console.log("Clicked! count:" + ++count);
        // Add your custom logic here
    });
});
    </script>
</html>

and the console output when I click on anchors:
Clicked! count:1
Clicked! count:2
Clicked! count:3
Clicked! count:4
Clicked! count:5
Clicked! count:6
Clicked! count:7
Clicked! count:8
 
Share this answer
 
v2
Comments
Graeme_Grant 24-May-23 1:58am    
I popped this into a test page and works fine. I've updated my solution with the full test.

The anchor (obj in the update) variable has all the information that you require. Set a breakpoint on the console.log in the browser debug console, then you can inspect the properties... if you need specific info, then you need to set a unique value to the id property of the anchor. eg:
<a href="#" class="my_anchor" id="anchor_8">anchor 8</a>
Graeme_Grant 24-May-23 2:16am    
The code that I posted above works. So, create a new page with the exact html + code as posted above and run it.

As for the warning for count I am not surprised, however is only there to demonstrate that the click event only fires when clicked on. You need to replace it with your own code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900