Silver challenge - what am I doing wrong?

Hi all,

For the silver challenge, I tried the following - what am I doing wrong? The console is giving me an error telling me that links.forEach(addLinkClickHandler) is not a function. I cannot figure out why…
Thanks for looking.

function addLinkClickHandler() {
addEventListener('click', function(event) {
event.preventDefault();
});
}

function getLinksArray() {
var anchorsArray = document.getElementsByTagName('a');
return anchorsArray;
}

function initializeEvents() {
var links = getLinksArray();
links.forEach(addLinkClickHandler);
}

initializeEvents();

You’re almost there!

The error message is telling you that the value returned by document.getElementsByTagName does not have a forEach method.

But, this value can be converted to something that does have a forEach.

You’ll want to refer back to how you wrote the getThumbnailsArray function earlier in the chapter for an example of how to do the conversion.

Cheers,
Chris

Hi Chris -

I’m doing a face palm right now. :slight_smile:

For some reason, I had it in my head that document.getElementByTagName returns an array, and therefore no conversion was needed. Now the HTMLCollection is converted to an array, and life is good! :tada:

Thanks for the little push that I needed, and for writing such an enjoyable and easy-to-learn-from book!

Regards,
Mike

Hey @mididoc @chrisaquino1 I’m still unable to get the silver challenge to work for me. Any ideas? I thought a fresh pair of eyes might help. Checkout this screencast of me implementing the silver challenge. What I’m I doing wrong?

@michael93, I tested your code from the screenshot you provided and when clicking on a link, nothing happened as it should be with the functions you wrote. I was using Google for testing. I also used Bing and a couple of others as well. When looking at the elements in the developer tools, Google and Bing use a different way of showing links compared to DuckDuckGo. Hopefully this helps. :grinning: