How can I use javascript to disable all links on page except those with a particular class

Viewed 33

sorry I am new to javascript but i'm working on a wordpress website and have to disable all the links on the home page from being clickable except the links with a particular class e.g. 'clickable'. As long as the link has that class, it becomes clickable.

I have been tweeking the below script but do not know how to introduce the class comparism, can anyone help me with this?

var links = document.getElementsByTagName('a');
document.addEventListener('click', function (e) {
            if (e.target.tag === links.tag) {
                e.preventDefault();
                document.addEventListener('contextmenu', 
                event => event.preventDefault());
                el_down.innerHTML = "Right click disabled";
            }
        }); 
1 Answers

This did it:

  document.body.addEventListener('click', function (event) {
  // filter out clicks on any other elements
  if (event.target.nodeName == 'A' && event.target.getAttribute('aria-disabled') == 'true') {
  console.log("True")
  }
  else {
  event.preventDefault();
  document.addEventListener('contextmenu', 
  event => event.preventDefault());
  el_down.innerHTML = "Right click disabled";
  }
});
Related