Eventlistener does not work with 2 or more inputs

Asked

Viewed 30 times

1

So friends, I have a page with a for that makes create several Formulars. In each form I have an input file, which I want to handle individually. However, when I try to use an eventlistener, it only works with the first for input. I don’t know how I can make it work for everyone. I made a Jsfiddle illustrating my problem here. Note that Alert is only triggered in the first case, in the other case nothing happens.

Does anyone have a solution?

EDIT fiddle link https://jsfiddle.net/2wapy6uo/

  • I guess your fiddle didn’t come...

  • #Cadeofiddle? haha

  • ready haha, so much design that the head can’t handle

1 answer

2


You need to use querySelectorAll and use a each for each of the elements, as in the below:

var inputs = document.querySelectorAll('input.sliderAluno');

[].forEach.call(inputs, function(input) {
    input.onchange= function()
    {
        alert("event");
    }
});
  • didn’t work here.

  • I updated the answer, look now

  • It worked fine, thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.