Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:42:15+00:00 2026-06-11T16:42:15+00:00

I am trying to teach myself general web development skills. I am trying to

  • 0

I am trying to teach myself general web development skills. I am trying to create a image upload with preview functionality using HTML5 FileAPI.

Till now, I have created a file input which shows the preview of image when selected.

Html mark up is below:

<div>
        <!-- Photos -->
        <fieldset>
            <legend>PropertyPhotos</legend>
            <div class="upload-box" id="upload-box-1">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_1" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File1)
                    @Html.ValidationMessageFor(model => model.File1)
                </div>
            </div>

            <div class="upload-box" id="upload-box-2">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_2" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File2)
                    @Html.ValidationMessageFor(model => model.File2)
                </div>
            </div>

            <div class="upload-box" id="upload-box-3">
                <div class="preview-box">
                    <img alt="Field for image cutting" id="preview_3" src="@Url.Content("~/Content/empty.png")" />
                </div>
                <div>
                    @Html.FileFor(model => model.File3)
                    @Html.ValidationMessageFor(model => model.File3)
                </div>
            </div>
        </fieldset>
    </div>

The Jquery to show preview and then display the next “upload-box” is as follows:

<script type="text/javascript">
        $(document).ready(function () {
            // show first box
            $("#upload-box-1").fadeIn();

            //Get current & next step index
            var stepNum = $('div.upload-box').attr('id').replace(/[^\d]/g, '');
            var nextNum = parseInt(stepNum)+1;

            //Get the preview image tag
            var preview = $('#preview_'+stepNum);

            //Load preview on file tag change and display second upload-box
            $('#File'+stepNum).change(function (evt) {
                var f = evt.target.files[0];
                var reader = new FileReader();

                if (!f.type.match('image.*')) {
                    alert("The selected file does not appear to be an image.");
                    return;
                }

                reader.onload = function (e) { preview.attr('src', e.target.result); };
                reader.readAsDataURL(f);

                //Show next upload-box
                $("#upload-box-" + nextNum).fadeIn();
            });
        });
    </script>

However, this code only first for the first time … i.e. on selecting a file – It shows a preview and then shows the next “upload-box”. However, when I browse using the second file it doesn’t show any preview.

From what I have ready, I need to close the Jquery function so that it can be initialised again but I am not sure how to do that.

My other guess is that this JQuery is only working on document load and I need to associate it with file click.

Any help will be grateful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:42:17+00:00Added an answer on June 11, 2026 at 4:42 pm

    Your code is executed once, when the DOM is ready the first time the page is loaded, because the page doesn’t ever reload and therefore will only bind a change event handler to the element with id File1. When you switch to using the file input with id File2 there’s no event handler bound to it, so selecting a file does nothing.

    You’ll need to bind the event handler to the other file input elements as well, possibly using an attribute-starts-with selector:

    $('[id^="File"]').change(...);
    

    or by adding a class to each of those elements and selecting based on that.

    You’ll also need to handle incrementing the value of nextNum when appropriate, so that subsequent calls display the next set of elements correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to teach myself AJAX/Web services using C# and javascript. And I think
I'm currently trying to teach myself PHP by doing a small image-processing type project
I'm trying to teach myself MVC3. Im converting from webforms. I need to create
I'm trying to teach myself python using Google's AppEngine , and I can't get
I am currently trying to teach myself C++ and programming in general. So as
I am trying to teach myself LRU algorithm using this youtube video . In
I'm trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I've
I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to
So I'm trying to teach myself object oriented programming using VB.net, and Although I
I'm trying to teach myself to create android app in eclipse. My project is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.