Get input file with several classes

0

I have an input file with two classes.

<input type="file" class="upload upload-logo" />
<input type="file" class="upload upload-rules" />
<input type="file" class="upload upload-image" />

I want to create a generic code for all inputs. This works, but I have to create 1 block for each input:

$("input[class=upload-logo]").change(function() {
            ...
        });

I've tested it like this, but it does not work:

$("input[class=upload]").change(function() {
            ...
        });
    
asked by anonymous 05.09.2016 / 12:17

1 answer

1

The way you are selecting is not the most conventional, if you want to select elements from a class you can simply:

$('.upload').on('change', function() {
  alert($(this).prop('class')); // só a titulo de exemplo, para ver o input em que selecionou o ficheiro
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="file" class="upload upload-logo" />
<input type="file" class="upload upload-rules" />
<input type="file" class="upload upload-image" />

To make it clear, yours was not giving because you were not using the selector correctly:

$('input[class^="upload"]').on('change', function() {
  alert($(this).prop('class')); // só a titulo de exemplo, para ver o input em que selecionou o ficheiro
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="file" class="upload upload-logo" />
<input type="file" class="upload upload-rules" />
<input type="file" class="upload upload-image" />

REFERENCE

    
05.09.2016 / 12:24