/*** @param {domElement} input - The input element* @param {string} typeData - The type of data to be return in the event object.*/function loadFileFromInput(input,typeData) {var reader,fileLoadedEvent,files = input.files;
if (files && files[0]) {reader = new FileReader();
reader.onload = function (e) {fileLoadedEvent = new CustomEvent('fileLoaded',{detail:{data:reader.result,file:files[0]},bubbles:true,cancelable:true});input.dispatchEvent(fileLoadedEvent);}switch(typeData) {case 'arraybuffer':reader.readAsArrayBuffer(files[0]);break;case 'dataurl':reader.readAsDataURL(files[0]);break;case 'binarystring':reader.readAsBinaryString(files[0]);break;case 'text':reader.readAsText(files[0]);break;}}}function fileHandler (e) {var data = e.detail.data,fileInfo = e.detail.file;
img.src = data;}var input = document.getElementById('inputId'),img = document.getElementById('imgId');
input.onchange = function (e) {loadFileFromInput(e.target,'dataurl');};
input.addEventListener('fileLoaded',fileHandler)
function init() {$("img[data-type=editable]").each(function (i, e) {var _inputFile = $('<input/>').attr('type', 'file').attr('hidden', 'hidden').attr('onchange', 'readImage()').attr('data-image-placeholder', e.id);
$(e.parentElement).append(_inputFile);
$(e).on("click", _inputFile, triggerClick);});}
function triggerClick(e) {e.data.click();}
Element.prototype.readImage = function () {var _inputFile = this;if (_inputFile && _inputFile.files && _inputFile.files[0]) {var _fileReader = new FileReader();_fileReader.onload = function (e) {var _imagePlaceholder = _inputFile.attributes.getNamedItem("data-image-placeholder").value;var _img = $("#" + _imagePlaceholder);_img.attr("src", e.target.result);};_fileReader.readAsDataURL(_inputFile.files[0]);}};
//// IIFE - Immediately Invoked Function Expression// https://stackoverflow.com/questions/18307078/jquery-best-practises-in-case-of-document-ready(
function (yourcode) {"use strict";// The global jQuery object is passed as a parameteryourcode(window.jQuery, window, document);}(
function ($, window, document) {"use strict";// The $ is now locally scoped$(function () {// The DOM is ready!init();});
// The rest of your code goes here!}));
function readURL(input) {if (input.files && input.files[0]) {var i;for (i = 0; i < input.files.length; ++i) {var reader = new FileReader();reader.onload = function (e) {$('#form1').append('<img src="'+e.target.result+'">');}reader.readAsDataURL(input.files[i]);}}}
//profile_change is the id of the input field where we choose an imagedocument.getElementById("profile_change").addEventListener("change", function() {
//Here we select the first file among the selected files.const file = this.files[0];
/*here i used a label for the input field which is an image and this image willrepresent the photo selected and profile_label is the id of this label */const profile_label = document.getElementById("profile_label");
//Here we check if a file is selectedif(file) {//Here we bring in the FileReader which reads the file info.const reader = new FileReader();
/*After reader loads we change the src attribute of the label to the url of thenew image selected*/reader.addEventListener("load", function() {dp_label.setAttribute("src", this.result);})
/*Here we are reading the file as a url i.e, we try to get the location of thefile to set that as the src of the label which we did above*/reader.readAsDataURL(file);
}else {//Here we simply set the src as default, whatever you want if no file is selected.dp_label.setAttribute("src", "as_you_want")}});