0

I am working on a site with bootstrap, where the input file does not show the selected file name. So I have a script that does the same.

But here it does not work with two inputs, so how to use the same with two inputs (two files upload).

Multiple inputs with same script

Basically, I want to use the same script for two inputs. I hope you got what I want.

I don't know how to use IDs or other ways to make both inputs separate.

$(document).ready(function() {
  $('input[type="file"]').change(function(e) {
    var display = e.target.files[0].name;
    $("h5").text(display + ' is the selected file.');

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="profile_image" class="custom-file-label">Profile image</label>
<input files="true" class="custom-file-input" id="img1" name="profile_image" type="file">
<h5 style="color: rgb(4, 158, 4);"></h5>

<label for="profile_image" class="custom-file-label">Profile image</label>
<input files="true" class="custom-file-input" id="img1" name="profile_image" type="file">
<h5 style="color: rgb(4, 158, 4);"></h5>

1
  • Well, one way would be to use e.target.nextSibling.innerText instead of $("h5") in the text assignment. By using the $("h5") selector, you've targeted ALL h5 elements. Commented Jan 10, 2021 at 6:16

1 Answer 1

4

Select the h5 after the input, not all h5 elements.

$(document).ready(function() {
  $('input[type="file"]').change(function(e) {
    var display = this.files[0].name;
    $(this).next("h5").text(display + ' is the selected file.');

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="profile_image" class="custom-file-label">Profile image</label>
<input files="true" class="custom-file-input" id="img1" name="profile_image" type="file">
<h5 style="color: rgb(4, 158, 4);"></h5>

<label for="profile_image" class="custom-file-label">Profile image</label>
<input files="true" class="custom-file-input" id="img1" name="profile_image" type="file">
<h5 style="color: rgb(4, 158, 4);"></h5>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.