1

THE CODE

const copy = document.querySelectorAll('.copy-me');

copy.addEventListener('copy', function(){
    console.log('OI! My content has copyright!');
    alert(' OI! My content has copyright! ');
});

THE ERROR Uncaught TypeError: copy.addEventListener is not a function at sandbox.js:3

NO ERROR CODE

const copy = document.querySelector('.copy-me');

copy.addEventListener('copy', function(){
    console.log('OI! My content has copyright!');
    alert(' OI! My content has copyright! ');
});

I want to use multiple class that is why i want ".querySelectorAll"

1 Answers1

0

Your mistake is obvious since you are accessing the collection. Method forEach() will help you as one of many options.

The collection needs to define the current item. Like this:

copy.forEach(function(copy_current) {
  copy_current.addEventListener('copy', function(){
  ...

Use this complete code:

const copy = document.querySelectorAll('.copy-me');

copy.forEach(function(copy_current) {
  copy_current.addEventListener('copy', function(){
    console.log('OI! My content has copyright!');
    alert(' OI! My content has copyright! ');
  });
});
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25