Removing inline JS


I want to remove inline JS in the following Codepen

I'm sure I don't have to put an event listener on each <li> (there are 11 here but 78 on the real site).

But how? Do I need to use data attributes? Can I put a listener on the containing div and check which list item was clicked on?


The following code adds a click event listener to each anchor:

var links = document.querySelectorAll("#card-layout a");
links.forEach(function (link, i) {
  link.addEventListener("click", function () {
    return false;

That lets you remove the HTML inline event attribute from all of the links.


Ah, as simple as that. Many thanks Paul!


One thing, I notice that despite the return false; the page skips to the top when a card is clicked on.


That's because return false doesn't prevent that from occurring.

When you want to prevent the default action from occurring, calling preventDefault on the event is what's used for that instead.

The pen at has been updated to do just that.


Ah, that's only with inline JS then?

Thanks for the revised Pen.


Sorry, wrong. Returning false is not only for inline JS.
Events and default actions can be a complex issue, so it's been well covered at


Hi there gandalf458,

check out the attachment to see greatly simplified code... (6.7 KB)



My brain hurts after reading that!

Thanks coothead. I suspected I could simplify it!


This attachment does away with the image preload and the CSS in the script. :winky: (7.5 KB)


JavaScript not working in Safari 9

I have just discovered that my two versions don't seem to work on Apples (Macs and iPhones).

closed #12

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.