Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
javascript : Identify the form in the loop after submission?

I have a loop that creates 4 forms in the view And I put a button for each form to submit the form? What solution would you suggest to find out which form was submitted? Html :

@foreach (var item in 4){
<form id="Form_ImgGallery" method="post">
    <div class="box_galleryimg col-lg-3 col-md-3 col-sm-6 col-xs-12">                       
        <input type="file" class="form-control">
        <div class="form-group">
            <input  type="submit" class="btn btn-info waves-effect" value="upload">
        </div>
    </div>
</form>

}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Maybe you should give each of the forms a different id. and then via id you can find out which form was submitted. here is a sample for getting idea.

const form = document.querySelector("#Form_ImgGallery");
form.addEventListener("submit", function (e) {
    e.preventDefault();
    console.log(e.target.attributes.id.value);  //Form_ImgGallery
});
about 4 years ago · Juan Pablo Isaza Report

0

The issue you have is that your loop is creating multiple elements with the same id, which is invalid. Within a single page, there can be no duplication of id. If you want to group elements, then use a class. This is what I've done in the example below.

From there you can use the this keyword in the event handler to refer to the specific form element which raised the submit event, and get information from it, or one of the form fields it contains.

$('.Form_ImgGallery').on('submit', e => {
  e.preventDefault(); // stop the form submitting in this example
  let $form = $(e.target);
  let file = $form.find('input[type="file"]').val();
  console.log(file);
});

/* plain JS version:
document.querySelectorAll('.Form_ImgGallery').forEach(form => {
  form.addEventListener('submit', e => {
    e.preventDefault(); // stop the form submitting in this example
    let file = e.target.querySelector('input[type="file"]').value;
    console.log(file);
  });
});
*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<form class="Form_ImgGallery" method="post">
  <div class="box_galleryimg col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <input type="file" class="form-control">
    <div class="form-group">
      <button type="submit" class="btn btn-info waves-effect">upload</button>
    </div>
  </div>
</form>

<form class="Form_ImgGallery" method="post">
  <div class="box_galleryimg col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <input type="file" class="form-control">
    <div class="form-group">
      <button type="submit" class="btn btn-info waves-effect">upload</button>
    </div>
  </div>
</form>

<form class="Form_ImgGallery" method="post">
  <div class="box_galleryimg col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <input type="file" class="form-control">
    <div class="form-group">
      <button type="submit" class="btn btn-info waves-effect">upload</button>
    </div>
  </div>
</form>

<form class="Form_ImgGallery" method="post">
  <div class="box_galleryimg col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <input type="file" class="form-control">
    <div class="form-group">
      <button type="submit" class="btn btn-info waves-effect">upload</button>
    </div>
  </div>
</form>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!