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

196
Views
Extend a javascript function

I have a function that I want to use it more than once, but i use double code for that. How can I extend the function for other input fields. THe function is adding a field in a form till maximul of input fields is reached. I want to use to other input fields in the same app without copy paste the code. Here is my function and what I tried to do:

function addTFS (maxField, addButton, link, fieldHTML){
    $(document).ready(function addt() {
        // Input fields increment limitation
        var maxField = 5;
        // Add button selector
        var addButton = $(".add_button");
        // Input field wrapper
        var link = $("#link_tbl");
        // New input field html
        var fieldHTML =
            '<tr><td><label for="tfs_link"><i class="fas fa-minus-circle remove_button"></i></label><input type="url" name="tfs" required/></td></tr>';
        // Initial field counter is 1
        var x = 1;

        // Once add button is clicked
        $(addButton).click(function () {
            // Check maximum number of input fields
            if (x < maxField) {
            // Increment field counter
            x++;
            // Add field html
            $(link).append(fieldHTML);
            }
        });

        // Once remove button is clicked
        $(link).on("click", ".remove_button", function (e) {
            e.preventDefault();
            // Remove field html
            $(this).closest("tr").remove();
            // Decrement field counter
            x--;
        });
        
    });
};

addTFS ();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't understand what you exactly mean by extending function. Do you want to add a decorator as we do in python?

But here is my approach to solving your problem, you can just call the base function into your wrapper like this:

//Base function
function square(input) {
    return input * input;
}

//Extended function
function modify(input) {
    return square(input) - 2;
}

console.log(modify(5));

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!