javascript - Jquery | How can I make a custom function and call it? -
i think quality of question low appreciate guys give me solution.
the part of code duplicated in 2 functions. want arrange long code short. below example want do.
void duplicatedfunc() { //some logic } void func1() { duplicatedfunc(); } void func2() { //func2 logic here.. duplicatedfunc(); }
how can in jquery ?? here jquery code.
$("#inputlogo").change(function(){ $("#logofile div.notyet").remove(); var file = this.files[0]; var fr = new filereader(); fr.onload = (function (file) { return function(e){ var div = document.createelement("div"); $(div).addclass("notyet").css({ margin : "30px" ,position : "relative" }); var html = ['<input type="hidden" name="inputlogo" value="' + file.name + '">' ,'<img src="" width="100%">' ,'<button type="button" class="close img-close" aria-label="close"><span aria-hidden="true">×</span></button>' ].join(""); $(div).append(html); $(div).find("button").click(function(){ $(this).parent().remove(); }); $(div).find("img").attr("src", e.target.result); $("#logofile").append(div); } })(file); fr.readasdataurl(file); }); $("#logo").change(function(){ // 새로운 이미지 추가할때 기존 이미지 삭제 (서버에서도 삭제) if ($('#logofile').find('img').length){ var targetdom = document.getelementbyid( "logofile" ); var targetinput = document.createelement("input"); targetinput.setattribute("name", "del_icon" ); targetinput.setattribute("type","hidden"); targetdom.appendchild(targetinput); //alert(targetinput.getattribute("name")); var filename = $('#logofile').find("input[name='inputlogo']").val(); //alert(filename); targetinput.setattribute("value", filename); $('#insertlogo').children().remove(); } $("#logofile div.notyet").remove(); var file = this.files[0]; var fr = new filereader(); fr.onload = (function (file) { return function(e){ var div = document.createelement("div"); $(div).addclass("notyet").css({ margin : "30px" ,position : "relative" }); var html = ['<input type="hidden" name="inputlogo" value="' + file.name + '">' ,'<img src="" width="100%">' ,'<button type="button" class="close img-close" aria-label="close"><span aria-hidden="true">×</span></button>' ].join(""); $(div).append(html); $(div).find("button").click(function(){ $(this).parent().remove(); }); $(div).find("img").attr("src", e.target.result); $("#logofile").append(div); } })(file); fr.readasdataurl(file); }); </script>
i want code looks clean. how can fix code?
var processfile = function processfile(file) { return (function(file) { return function(e) { var div = document.createelement("div"); $(div).addclass("notyet").css({ margin: "30px", position: "relative" }); var html = ['<input type="hidden" name="inputlogo" value="' + file.name + '">', '<img src="" width="100%">', '<button type="button" class="close img-close" aria-label="close"><span aria-hidden="true">×</span></button>'].join(""); $(div).append(html); $(div).find("button").click(function() { $(this).parent().remove(); }); $(div).find("img").attr("src", e.target.result); $("#logofile").append(div); } })(file) }; $("#inputlogo").change(function() { // stuff fr.onload = processfile; // stuff }); $("#logo").change(function() { // stuff fr.onload = processfile; // stuff });
Comments
Post a Comment