jquery - I want to change a profile picture when i select a image -
$('#profile_picture').on("change",function(){ var files = !!this.files ? this.files : [];
what above code !!this.files ? this.files : []
mean?
if (!files.length || !window.filereader) return; if (/^image/.test( files[0].type)){ var reader = new filereader(); reader.readasdataurl(files[0]); reader.onloadend = function(){ $("#imgchange").attr("src", "+this.result+"); } } });
that used check whether new fileapi supported. if new file api not supported file input's files
property undefined !this.files
false , when add 1 more !
false again negated true
$('#profile_picture').on("change", function () { // files new property new file api, if if not supported assign empty array value of files var files = !! this.files ? this.files : []; //if there no files , filereader not supported return if (!files.length || !window.filereader) return; if (/^image/.test(files[0].type)) { var reader = new filereader(); reader.readasdataurl(files[0]); reader.onloadend = function (event) { $("#imgchange").prop("src", event.target.result); } } });
Comments
Post a Comment