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

123
Views
Javascript How to Pass a parameterized function as Click Event Handler Without firing it on page load

Problem : The function that I want to invoke only when submit button is clicked is invoked immediately on document/page load. I only want the function to fire when I click the submit button.

This function requires a formId parameter. I have many forms with different formId's I want to re-use the function and just supply the formId as argument to it. The form id will be used by some ajax call.

Example of re-using it:

buttonA.on('click', loadPageOfSelectedNavItem('#myFormIdA') );
buttonB.on('click', loadPageOfSelectedNavItem('#myFormIdB') );
buttonC.on('click', loadPageOfSelectedNavItem('#myFormIdC') );

//.. and so on..

I did some research in SO and found related questions & answers where they suggested to just supply the function name without parentheses.

As in buttonA.on('click', loadPageOfSelectedNavItem);

However, it won't work for my purpose because I need to supply/pass the formId as argument to the function.

Can you suggest possible solutions?

Below is my current code.

(function(){
    buttonA.on('click', loadPageOfSelectedNavItem('#myFormIdA')); //this fires the function on document ready
    buttonB.on('click', loadPageOfSelectedNavItem('#myFormIdB')); //this fires the function on document ready
});

function loadPageOfSelectedNavItem(formId){
    var form = $(formId);
    $.ajax({
        url : form.attr('action'),
        data: form.serialize(),
        type: "post",
        success: function(response, status, xhr){
            $('#divContainer').html(response); //response is in html format
        }, error: function(error){
            console.log(error);
        }
    });
}

Thank you.

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

0

How about something like:

buttonA.click({id:"#myFormIdA"}, function(event) {
  loadPageOfSelectedNavItem(event.data.id)
});
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!