Skip to content

Instantly share code, notes, and snippets.

@AngeloMerlo
Last active December 20, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AngeloMerlo/6065224 to your computer and use it in GitHub Desktop.
Save AngeloMerlo/6065224 to your computer and use it in GitHub Desktop.
DRY - altera o background de uma div sem fazer upload da imagem (ha, pegadinha do malandro)
$(document).ready(function(){
// faz a leitura do arquivo e atualiza a div
function readURL(input,div) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$(div).css('background-image', 'url(' + e.target.result + ')');
}
reader.readAsDataURL(input.files[0]);
}
}
// quando mudar o valor do campo atualiza
$('[id^=arquivo_premio_]').change(function(){
var id = $(this).attr('id').split("arquivo_premio_");
readURL(this,'#imgPromo_'+id[1]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment