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

  • Home
  • SEARCH
  • 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 3404094
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:20:26+00:00 2026-05-18T05:20:26+00:00

I am trying to allow multiple file uploads prior to a form post. What

  • 0

I am trying to allow multiple file uploads prior to a form post. What I’d like is for the user to only ever see one file upload element, and each time a file is selected, show a new <li> that contains the filename and an image/link to remove that specific file from the collection. There’s a jQuery MultiFile plugin that does what I want, but I can’t get the custom styling to work the way I want so I’m rolling my own.

So far I have the below code. It successfully adds the <li>, hides the file upload element with the newly selected file, and appends an empty file upload element to the page for the user to select a new file. I’m struggling to appropriately manage the removal of elements and, while it’s not that difficult, I’ve been staring at this long enough that now it just feels like I’m doing it all wrong. I’m hoping others might have some insight, tips to clean this up (i.e. make it more elegant), and such. Code below.

HTML:

<div id="product-image-area" class="group" style="border-bottom: none; padding-top: 0"> 
    <ul id="photos" class="nobull">    
     <li id="no-product-images-msg" class="" > 
            <%= Html.Image("no-photos.png") %> 
     </li> 
   </ul>
 </div> 
 <div id="upload-area"> 
    <h4 class="st">Upload an image</h4>
    <p class="note">Allowed file types are .gif, .jpg, .jpeg, and .png</p> 
    <p id="file-input" class="st sb"><input class="photo-upload" id="VenuePhotos_0" name="VenuePhotos[]" type="file" /></p> 
 </div>

Script:

$(function () {
     $('.photo-upload').live('change', function () {
         var fileCount = new Number($(this).parent().children('.photo-upload').length);
         $('#photos').append('<li id="venue_photo_' + (fileCount - 1) + '">' + $(this).val() + '<img title="' + (fileCount - 1) + '" src="/vh/public/images/icon-delete.png" class="delete"  /></li>');
         $(this).parent().append(
             $(this).clone().attr('id', 'VenuePhotos_' + fileCount)
         );
         $(this).hide();
     });
     $('.delete').live('click', function (e) {
         var index = $(e).attr('title');
         $('#file-input').children('.photo-upload').remove('#VenuePhotos_' + index);
         $('#photos').children().remove('#venue_photo_' + index);
     });
});
  • 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-05-18T05:20:27+00:00Added an answer on May 18, 2026 at 5:20 am

    The answer is the closure, one of the most powerful features of JavaScript. Functions that are inside others are able to access the variables of the enclosing functions.

    You could bind your delete function when a file input is added instead of using .live and dynamically generated ID’s:

    $('.photo-upload').live('change', function () {
         var li = $('<li />').text($(this).val()),
             img = $('<img src="/vh/public/images/icon-delete.png" class="delete" />'),
             input = $(this).clone();
    
         li.append(img);
         $('#photos').append(li);
         $(this).parent().append(input);
         $(this).hide();
    
         img.click(function() {
             input.remove();
             li.remove();
         });
     });
    

    In this example, the click handler for the delete button accesses the jQuery wrappers (for the two elements that must be removed) that were obtained in the parent function.

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

Sidebar

Related Questions

I'm trying to configure Apache to allow read only access and ask for user
I'm working on a forums system. I'm trying to allow users to see the
I'm trying to create a basic system that will allow only specific users to
I'm trying to make a webpage that allows the uploading of multiple files at
I am trying to progamrtaiclly allow write access to ASPNET account on a directory.
I'm trying to write a script to allow me to log in to a
I've been trying to figure out a regex to allow me to search for
I'm trying to create a function in C# which will allow me to, when
I'm trying to create a Regex usuable in C# that will allow me to
I am trying to find out which Hypervisor will allow me to grant access

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.