Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6215957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:08:56+00:00 2026-05-24T07:08:56+00:00

I have some jQuery codes, which is repeated again and again, i would like

  • 0

I have some jQuery codes, which is repeated again and again, i would like to reduce the code i am writing that is by converting it into functions. here is the codes i am using.

$('form#save-user button[name="save-user"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = "index.php?users&option=edit&user_id="+msg+'&msg=success';
            }
        }
    });
});

$('form#save-user button[name="save-user-close"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = 'index.php?users';
            }
        }
    });
});

$('form#save-user button[name="save-user-new"]').click(function() {
    var formData = 'option=saveuser&'+$('form#save-user').serialize();
    $.ajax({
        type: 'POST',
        url:  'process.php',
        data: formData,
        success: function(msg){
            if(msg === 'empty') {
                alert('Required Values Missing');
            } else if(msg === 'duplicateEmail'){
                alert('Email already exist');
            } else {
                window.location = 'index.php?users&option=create';
            }
        }
    });
});

I would like to know few things,

a) With reference to above code, how do i convert it to
function, as the code have very few changes like, selector name and
url of window.location.

b) what do i call the code below, is it the function? function on
the go? or dynamic function?

$('selector').event(function(){
     //jQuery Code in wake of event being triggered.
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:08:56+00:00Added an answer on May 24, 2026 at 7:08 am

    I would write a small plugin for it:

    (function ($) {
    
    jQuery.fn.myClick = function (destination) {
        this.click(function () { 
            var formData = 'option=saveuser&'+$('form#save-user').serialize();
            $.ajax({
                type: 'POST',
                url:  'process.php',
                data: formData,
                success: function(msg){
                    if(msg === 'empty') {
                        alert('Required Values Missing');
                    } else if(msg === 'duplicateEmail'){
                        alert('Email already exist');
                    } else {
                        window.location = destination(msg);
                    }
                }
            });
        });
    }
    
    }(jQuery));
    

    Then to call it you would simply do:

    $('form#save-user button[name="save-user-close"]').myClick(function() {
        return 'index.php?users&option=create';
    });
    

    and

    $('form#save-user button[name="save-user"]').myClick(function (msg) {
        return "index.php?users&option=edit&user_id="+msg+'&msg=success';
    });
    

    You should be able to see where we’re going here; we’re adding parameters to the small method we’ve created, where you can specify what changes between each call.

    Because in this instance, the window.location depends on the AJAX response (and we don’t know the response when we invoke the function!), we can’t simply provide a string (or something similar) as the parameter. Instead, we pass a function which we invoke once the AJAX response is received, provided the msg as a variable; and rely on the function to provide a string which we set to the window.location.


    As for your second question, you pass an event handler to the jQuery event methods; an event handler will be a function reference (usually a reference to an anonymous function)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery code that highlights which radio button is checked by turning
I have this jQuery code which replaces some HTML character codes with their equivalent:
I have some code in jquery which looks like this: $(#filterZaal).attr(disabled, disabled); $(#filterGenre).attr(disabled, disabled);
I have some jquery code which I am trying to translate to YUI. I
I have some jQuery code. I have called an Ajax function file, file.php, that
I have some jquery code that is doing an ajax lookup and returning comma
I have some jQuery code that highlights a link when clicked, and changes the
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some jQuery/JavaScript code that I want to run only when there is
Trying to get comfortable with jQuery and I have encountered some sample code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.