Javascript ImagePreview












-1















I need a preview of an image after it got upload via <input type="file onchange="readURL(this);">.



I have found a solution on the internet, but that doesn't work for me:



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
};

reader.readAsDataURL(input.files[0]);
}
}


"#blah" is the ID of the image(it is shown with a placeholder at first).
The Error I get when opening the Console, after I uploaded an image : Uncaught ReferenceError: $ is not defined at FileReader.reader.onload ((index):156)



But it works here : https://codepen.io/mobifreaks/pen/LIbca



Please give me a short explanation why this doesn't work for me and the solution to it.






  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>












share|improve this question




















  • 1





    You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

    – Just code
    Nov 20 '18 at 8:16













  • Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

    – Jai
    Nov 20 '18 at 8:18


















-1















I need a preview of an image after it got upload via <input type="file onchange="readURL(this);">.



I have found a solution on the internet, but that doesn't work for me:



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
};

reader.readAsDataURL(input.files[0]);
}
}


"#blah" is the ID of the image(it is shown with a placeholder at first).
The Error I get when opening the Console, after I uploaded an image : Uncaught ReferenceError: $ is not defined at FileReader.reader.onload ((index):156)



But it works here : https://codepen.io/mobifreaks/pen/LIbca



Please give me a short explanation why this doesn't work for me and the solution to it.






  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>












share|improve this question




















  • 1





    You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

    – Just code
    Nov 20 '18 at 8:16













  • Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

    – Jai
    Nov 20 '18 at 8:18
















-1












-1








-1








I need a preview of an image after it got upload via <input type="file onchange="readURL(this);">.



I have found a solution on the internet, but that doesn't work for me:



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
};

reader.readAsDataURL(input.files[0]);
}
}


"#blah" is the ID of the image(it is shown with a placeholder at first).
The Error I get when opening the Console, after I uploaded an image : Uncaught ReferenceError: $ is not defined at FileReader.reader.onload ((index):156)



But it works here : https://codepen.io/mobifreaks/pen/LIbca



Please give me a short explanation why this doesn't work for me and the solution to it.






  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>












share|improve this question
















I need a preview of an image after it got upload via <input type="file onchange="readURL(this);">.



I have found a solution on the internet, but that doesn't work for me:



function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
};

reader.readAsDataURL(input.files[0]);
}
}


"#blah" is the ID of the image(it is shown with a placeholder at first).
The Error I get when opening the Console, after I uploaded an image : Uncaught ReferenceError: $ is not defined at FileReader.reader.onload ((index):156)



But it works here : https://codepen.io/mobifreaks/pen/LIbca



Please give me a short explanation why this doesn't work for me and the solution to it.






  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>








  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>





  function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#preview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}

#preview {
max-width: 150px;
max-height: 150px;
}

<input type="file" id="files" name="file" style="margin-left:1px" onchange="readURL(this);"/><br>
<img id="preview" src="" alt=""/>






javascript html image






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 9:30









fiza khan

913419




913419










asked Nov 20 '18 at 8:13







user10507326















  • 1





    You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

    – Just code
    Nov 20 '18 at 8:16













  • Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

    – Jai
    Nov 20 '18 at 8:18
















  • 1





    You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

    – Just code
    Nov 20 '18 at 8:16













  • Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

    – Jai
    Nov 20 '18 at 8:18










1




1





You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

– Just code
Nov 20 '18 at 8:16







You are just missing <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> reference into your file and it should work. This can be achieved without jquery too

– Just code
Nov 20 '18 at 8:16















Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

– Jai
Nov 20 '18 at 8:18







Either jQuery is missing or you are not aware that $ is the alias for the jQuery and you forgot to add it in the page. Instead just translate it in javascript syntax.

– Jai
Nov 20 '18 at 8:18














1 Answer
1






active

oldest

votes


















1














You must import the jquery.min.js






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388727%2fjavascript-imagepreview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You must import the jquery.min.js






    share|improve this answer




























      1














      You must import the jquery.min.js






      share|improve this answer


























        1












        1








        1







        You must import the jquery.min.js






        share|improve this answer













        You must import the jquery.min.js







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:46









        M. FM. F

        276




        276
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388727%2fjavascript-imagepreview%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud