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

191
Views
JavaScript addEventListner form within a form

I have a Form made up of multiple fields.

Within this form, I am adding two new fields that I like to be able to keep adding values to which get submitted to the database.

The primary form is used to populate a city name, address, etc. the new fields are used to populate event names and dates.

For example, I add Manchester as the city and then I'd like to add as many events to Manchester as I like.

At the moment I have a JavaScript addEventListener setup for the submit button of the primary form which will POST all for the fields i specify, but this refreshes the page every time i SUBMIT

I need to keep the city name field populated so that each event added will contain the city name and the additional event name and event date fields.

Ultimately I want a table with City, EventName and EventDate and a table with the city details.

So I'm wondering how i do this with JavaScript to only submit the city, event name and event date?

I tried to create a new form within a form and reference that with a different js file, but this triggers when the primary form submit is triggered.

var event_form = document.getElementById("add-event");
event_form.addEventListener("submit", function (e) {
    e.preventDefault();

    if (validated(this)) {
        this.classList.add("was-validated");
        var e_eventname = document.getElementById("project-event-name").value;
        var e_eventdate = document.getElementById("project-event-date").value;
        //var e_cityname = document.getElementById("city-name").value;

        const csrftoken = document.getElementsByName("csrfmiddlewaretoken")[0].value;

        const formdata = new FormData();

        formdata.append("event_name", e_eventname);
        formdata.append("event_date", e_eventdate);
        //formdata.append("city_name", e_name);


        const xhr = new XMLHttpRequest();
        xhr.open("POST", "/apps/add_event");
        xhr.setRequestHeader("X-CSRFToken", csrftoken);
        xhr.send(formdata);
        xhr.onload = () => {
            window.location.reload();
        };
    }
})

So i somehow need to setup the JS to trigger on a seperate button that will only POST the specific fields.

about 4 years ago · Juan Pablo Isaza
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!