I have a file uploader on my website and I’m trying to keep an accurate count of the files uploaded, and display that number in an input using JavaScript / jQuery. Find a live version of an identical form Here
Here’s my HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Linux (vers 25 March 2009), see www.w3.org" />
<title></title>
</head>
<body>
<div>
<form action="/?gf_page=preview&id=2" enctype="multipart/form-data" method=
"post">
<div>
<h3>Samples</h3>
</div>
<div>
<ul>
<li>
<label for="input_2_17">Upload*</label>
<div>
<input name="files" type="file" data-id="17" data-limit="50"
data-max_upload_size="512" data-allowed_extensions="jpg,png,gif"
aria-required="true" aria-invalid="false" /><input name="input_17" type=
"hidden" value="" aria-required="true" />
</div>
<div>
<p>Upload your files</p>
</div>
</li>
<li>
<label for="input_2_1">Paper Type*</label>
<div>
<select name="input_1" aria-required="true" aria-invalid="false">
<option selected="selected" value="">
Choose a paper or canvas type
</option>
<option value="Matte Paper|5">
Paper 1
</option>
<option value="Gloss Paper|5">
Paper 2
</option>
<option value="Matte Canvas|5">
Paper 2
</option>
<option value="Gloss Canvas|5">
Paper 3
</option>
</select>
</div>
<div>
Choose paper type.
</div>
</li>
<li>
<label for="input_2_12">Quantity*</label>
<div>
<input name="input_12" type="text" value="" aria-required="true"
aria-invalid="false" aria-describedby="gfield_description_2_12" />
</div>
<div>
The quantity is calculated automatically based on how many images you have
uploaded.
</div>
</li>
<li>
<label for="input_2_27">Total</label>
<div>
<input name="input_27" readonly="readonly" type="text" value=""
aria-invalid="false" />
</div>
</li>
<li>
<label for="input_2_28">Email</label>
<div>
<input name="input_28" type="text" value="" />
</div>
<div>
This field is for validation purposes and should be left unchanged.
</div>
</li>
</ul>
</div>
<div>
<input type="submit" value="Submit" /><input name="is_submit_2" type="hidden"
value="1" /><input name="gform_submit" type="hidden" value="2" /><input name=
"gform_unique_id" type="hidden" value="" /><input name="state_2" type="hidden"
value=
"123" /><input name="gform_target_page_number_2"
type="hidden" value="0" /><input name="gform_source_page_number_2" type="hidden"
value="1" /><input name="gform_field_values" type="hidden" value="" />
</div>
</form>
</div>
</body>
</html>
When a file is uploaded, it gets added as a <li class="fileuploader-item"></li>
within <ul class="fileuploader-items-list">
. If it's deleted, it's removed from <ul class="fileuploader-items-list">
Here's my JavaScript so far:
$(document).on('change', 'input', function() {
var uploadcount = $('.fileuploader-item').length;
document.getElementById("input_2_12").value = uploadcount;
});
This counts the uploaded files and shows that number in my input.
However, you can also delete files you've uploaded to the form. If a file is deleted, the number in the input doesn't decrease. Would anyone show me how to keep an accurate count of every <li class="fileuploader-item"></li>
within <ul class="fileuploader-items-list">
?