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

  • Home
  • SEARCH
  • 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 7533577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:45:53+00:00 2026-05-30T05:45:53+00:00

I like to keep my code as DRY as possible and would like to

  • 0

I like to keep my code as DRY as possible and would like to know if the following is possible.
I’ll need to reuse the code below many times with the only difference being what I do in the following function.

.on("click", "a", function(e) {})

I could just duplicate the addToBooking function, give it a different name, make the slight change in the click handler but that feels wasteful and repetitive.

Can I perhaps pass a code block to the addToBooking function?
Or maybe there’s another cool, efficient way I’m not aware of.

The full code block

var addToBooking = function(that, event) {
  var left, top;

  event.stopPropagation(); 

  //Turn off all calendar tracking
  $(".track").off(); 

  //Get proper sticky positioning (Checks to make sure it wont display off screen)
  left = getPosition(".add_to_booking", "left");
  top = getPosition(".add_to_booking", "top");

  //Position sticky, display and listen for click event to see what to do next
  $(".add_to_booking").css({top: top, left: left})
                      .fadeIn('fast')
                      .on("click", function(e) { e.stopPropagation(); })
                      .on("click", "a", function(e) {
                        if($(this).text() === "YES") {
                          //Close dialog
                          closeTT();

                          //Open new add to booking box
                          addBooking(that, event);

                        } else {
                          closeTT();
                        }
                      });
}
  • 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-30T05:45:54+00:00Added an answer on May 30, 2026 at 5:45 am

    Pass in the function and assign it

    var addToBooking = function(that, event, custFnc) {
        ...
        ...
        .on("click", "a", custFnc );
    }
    

    Fiddle

    To pass parameters, you need to use call()

    jQuery( function(){
    
        function hey(evt, test){ 
            var text = jQuery(this).text();
            alert(text + ":" + test);
        }
    
    
        function addClick( custFnc ){ 
            var test=99;
            jQuery("#foo").on("click", 
                function(e){ 
                    custFnc.call(this, e,test); 
                } 
            );
        }
    
        addClick(hey);
    });
    

    ​
    Fiddle

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

Sidebar

Related Questions

I would like to keep the code readable by writing the actual code of
In general, I don't like to keep code (BaseClasses or DataAccess Code) in the
I keep seeing code that does checks like this if (IsGood == false) {
I'd like to keep my components/assemblies clearly separated from a source code point of
I would like to keep the overhead at a minimum. Right now I have:
I would like to keep a static counter in a garbage collected class and
I would like to keep a list of a certain class of objects in
<input type=radio value=1 id=baby> I'd like to keep this code like that. However, can
I am implementing a roulette wheel selection, and I would like to keep as
I'd like to change the code below to present a yes or no option

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.