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

210
Views
How can I make my HTML Form have an onsubmit event and an onclick action for its submit button?

So I currently have the following HTML form:

<form  id = "leisureExpenses" id = "leisureExpenses" style = "display:none" onsubmit='addExpense();'>
   <div class="row">
     <div>
     <label>Clothing type</label>
<input  name = "Activity" id = 'activity' style = "width: 24%;" type="text" placeholder="Enter the clothing type" id="exampleEmailInput">
      </div>
          </div>
            <div>
                <div>
              <label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Clothing place</label>
              <input name = "Place" id="place" style = "width: 24%;" type="text" placeholder="Enter the clothing place" id="exampleEmailInput">
               </div>
                              
            <label>Amount</label>
            <input name = "Amount" id="amnt" style = "width: 24%;" type="number" placeholder="Enter number" id="exampleEmailInput">
                 </div>
                        
                    
                    
<button type = 'submit' class="button-primary" onclick= "sample(this);return false;">Add expense</button>
<button type = 'button' class="button-primary" onclick="closeExpenseForm()">Close Form</button>

                    
 </form>

Here is the sample() function:

function sample(e) {
                          
const url = "https://script.google.com/macros/s/AKfycbw6tJQIK0izFU-Ehb4FF2rpct_fyqvqHDlIiv13eumC18y2Ds0HhNRtAHkiww1A/exec"
                          
const obj = [...e.parentNode].reduce((o, f) => (o[f.name] = f.value, o), {});
const query = new URLSearchParams(obj); 
fetch(url + "?" + query, { method: "POST" })
  .then((res) => res.json())
  .then((res) => {
   console.log(res);
   alert(res.result);
  });
}

Here is the addExpense() function:

function addExpense()
{

        clothingID={'id':Date.now(),'activityType':activityType.value, 'place': place.value, 'amnt':amnt.value, };
        leisureArray.push(clothingID);
        arrayLength++;
        showTotalBalance();
        showDifference();
        expenseHistory();    
        setItemInStorage();  
}

The sample() function seems to be overriding the addExpense() function. How do I fix this?

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!