Solution 1 :

Wrong selector possibly,

$(function() {
    if (document.querySelector('.pagination a')) {
        console.log("Well a single search came up fine");
    }
    [...document.querySelectorAll('.pagination a')].forEach(e => {
        console.log(e);

        alert(Object.prototype.toString.call(e)); // [object HTMLAnchorElement]
        e.addEventListener('click', function(ev) {
            alert('Click!');
            if (!this.classList.contains('collapsed')) {
                // Prevents last collapsable from being closed
                ev.stopPropagation();
            }
        });
    });
});

To maybe make sure; code runs fine on my end?

I tested this

    [...document.querySelectorAll('a')].forEach(e => {
        console.log(e);
        e.addEventListener('click', function(ev) {
            alert('Click!');
            if (!this.classList.contains('collapsed')) {
                // Prevents last collapsable from being closed
                ev.stopPropagation();
            }
        });
    });

in console and it turned every link to what you desired. So possibly a bad reference or something out of the blue! :X

Problem :

I have the below code in my project. I’m getting all anchor elements under .pagination and iterating over them and adding an event to each of them.

I added the alert in to prove that the elements have in fact loaded into the page and they are of the correct type (HTMLAnchorElement), however when I click on them, nothing fires. I even changed the event to various other types such as mousedown, select, etc.

$(function() {
    [...document.querySelectorAll('.pagination a')].forEach(e => {
        alert(Object.prototype.toString.call(e)); // [object HTMLAnchorElement]
        e.addEventListener('click', function(ev) {
            alert('Click!');
            if (!this.classList.contains('collapsed')) {
                // Prevents last collapsable from being closed
                ev.stopPropagation();
            }
        });
    });
});

Is anyone able to tell me why my events aren’t firing?

Comments

Comment posted by Teemu

As blanknamefornow has said in their answer, the code works as it is. You’ve to check, that there’s no overlaying transparent element on your links, or

Comment posted by liam-milbel

@Teemu There is no overlaying transparent element. There are other events on the links from Bootstrap that fire without issue and there is CSS hover effects that also work. I have used this exact snippet in another project for the same purpose and it works without issue.

Comment posted by minimal reproducible example

What ever it is, it can’t be reproduced by the provided code, like said, it works as it is. You’ve to build a

Comment posted by BGPHiJACK

Also another guess, [object HTMLAnchorElement] looks odd for an output.

Comment posted by Teemu

That output is expected, as the selector collects

Comment posted by BGPHiJACK

I’ll have to maybe say it’s a selector issue by far, worked perfectly with just ‘a’. 🙂

Comment posted by BGPHiJACK

In Console you can always go to Elements, and right click any of them Copy > Copy Selector. For fast querySelector lingo.

Comment posted by liam-milbel

Not using the wrong selector. As I stated in the question, the initial alert triggers fine. I have two anchors matching the selector and I get two alerts of

By