I needed to make an upload system, which when selecting the image, a preview would be shown in a box before being sent. I saw a code here in Stackoverflow and tried to adapt it. I would like to know if it is semantically correct, according to the language standards, and also, I would like an explanation on the code. (not JQUERY / JS mango).
Thank you all for helping :) Have a good weekend.
$('.arquivo').change(function(){
var preview = $('.enviar_screenshot button'); //BOTAO DE SELECIONAR ARQUIVO
var file = $('.arquivo')[0].files[0]; // O ARQUIVO
var reader = new FileReader();
var ext = ['jpg','jpeg','png']; // EXTENSÕES PERMITIDAS
var extArquivo = file.name.split(".").pop(); //PEGA A EXTENSÃO DO ARQUIVO
reader.onloadend = function () {
if(typeof ext.find(function(extt){return extArquivo == extt; }) == 'undefined') { // VERIFICA SE A EXTENSÃO NÃO É VÁLIDA, SE NÃO, REALIZA O PROCESSO NORMAL
$(preview).css({'background-image':'none', 'opacity':'1'});
$(preview).html('<i class="fas fa-plus"></i>');
} else {
$(preview).css({'background-image':'url(' + reader.result + ')', 'background-size':'cover', 'background-position':'center', 'opacity':'0.5'});
}
}
if (file) { // MOSTRA
reader.readAsDataURL(file);
$(preview).text("");
} else {
$(preview).css({'background-image':'none', 'opacity':'1'});
$(preview).html('<i class="fas fa-plus"></i>');
}
});