I have a form with a file input and 2 text boxes. All 3 inputs can be duplicated if more than one file needs to be uploaded. I am trying to add a clear button at the end of every div that will allow the user to clear the value of the file input. But for the life of me I cannot get it to work.
I am trying to use .last() but it doesn't seem to be selecting the last file input once the div has been duplicated.
My code is below.
<div class="certificates_list">
<input type="file" name="user_certificates[]" class="user_certificate" />
<input type="text" name="certificate_name[]" class="right certificate_name" placeholder="Certificate name" />
<input type="text" name="expiry_date[]" class="expiry_date" placeholder="DD/MM/YYYY" />
<button class="clear_certificate">Clear</button>
</div>
$(function() {
$('.clear_certificate').click(function(event) {
event.preventDefault();
$('input[type="file"].user_certificate').last().val('');
});
});
If I just have one div with inputs it clears it fine. Once the div has been duplicated the previous div won't clear anymore.
Any ideas? Thank you for any help, much appreciated!
EDIT
I have created a jsfiddle with my problem, hope this helps
.on()instead of.click()for the event, because it only registers what's already loaded in the DOM and not future elements.