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 8730227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:58:41+00:00 2026-06-13T08:58:41+00:00

I hope I can explain my self good. I want to create a function

  • 0

I hope I can explain my self good.
I want to create a function that has arguments which one of the arguments is a jQuery function that would be executed on event of an element that would be created after the call of the original function (it is very similar to dialog). Here is the code of what I am trying to do:

function messageBox(messageOptions) {
    var default_args = {
        'header': null,
        'text': null,
        'acceptButtonText': 'ok',
        'acceptButtonOnClickFunction': null,
        'cancelButtonText': 'cancel',
        'cancelButtonOnClickFunction': function() {
            $('#cancel_message_box_div_button_span').click(function() {
                $('.greyBackground').remove();
            })
        }
    };
    for (var index in default_args) {
        if (typeof messageOptions[index] == "undefined") messageOptions[index] = default_args[index];
    }

    putGrayBackground();
    var messageBoxElement = '<div class="messageBoxDiv">';
    messageBoxElement += '<legend class="messageBoxDivHeader">';
    messageBoxElement += messageOptions.header;
    messageBoxElement += '</legend>';
    messageBoxElement += '<div class="messageBoxDivText">';
    messageBoxElement += messageOptions.text;
    messageBoxElement += '</div>';
    messageBoxElement += '<div class="messageBoxDivButtons">';
    messageBoxElement += '<div id="cancel_message_box_div_button_span" class="messageBoxDivButtonSpan">';
    messageBoxElement += messageOptions.cancelButtonText;
    messageBoxElement += '</div>';
    messageBoxElement += '</div>';
    messageBoxElement += '</div>';
    $('.greyBackground').append(messageBoxElement);
}​

The code suppose to create an element that has a span which when it would be clicked will call the function under ‘cancelButtonOnClickFunction’ argument.
As you can see there are default parameters, one of them is ‘cancelButtonOnClickFunction’. After I have created the element I append it, I think it doesn’t works because the ‘cancelButtonOnClickFunction’ inner function is called before the element is created. I would ask how to make it work.

  • 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-06-13T08:58:42+00:00Added an answer on June 13, 2026 at 8:58 am

    The problem isn’t that your code is executed too early. In fact it isn’t executed at all! Let’s just get to fixing the code and along the way we will learn why the original your version didn’t work.

    First, we change something in your default arguments:

        [..]
        'cancelButtonOnClickFunction': function() {
                $('.greyBackground').remove();
            })
        [..]
    

    Basically, you would want your users to define their event handler (or callback) in exactly the same way they would if they were using jquery directly, so you should be doing the same thing in your default argument.

    Now here is the catch: you are not executing a function here. You are defining a function and assigning that function to the cancelButtonOnClickFunction property. This is possible because in javascript, functions are first class citizens. In less fancy words, that means that functions are “values” in much the same way numbers, strings and (other) objects are and as such, you can do a lot of things with them like using them as arguments or assigning them to variables. (In fact, in javascript function a() {alert('a')} is just another way to write var a = function() {alert('a')}, but I am getting way off track here.)

    Alright, so now that we got that piece of code down, we now need to make sure that the event is connected correctly. Because we aren’t stuck doing this at the top of the function, we can just do so after the html has been generated and we won’t have to worry about the assignment happening before the html is in place anymore:

        [..]
        $('.greyBackground').append(messageBoxElement);
        $('#cancel_message_box_div_button_span').click(messageOptions.cancelButtonOnClickFunction);
    }
    

    And that’s it, here we just supply the variable that holds the function instead of providing an anonymous function like we are used to when normally working with jquery. These two changes should solve the code and make sure it does what you want it to do. And hopefully my explanation along the way helped you understand a little more about the how and why.

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

Sidebar

Related Questions

I hope that somebody can help me. I want to redeclare js function by
I hope I can explain this well enough so that you can understand the
I hope I can explain this right I have two input fields that require
I hope I can explain my problem in a way that it's clear for
I hope this I can explain what I am trying to achieve: I want
I hope I can explain this properly. I have a grid that is part
Here's an interesting one... hope I can explain it well... I have a collection
I'm not very good in SQL stuff, so I hope someone can explain this
I hope I can explain this properly. I'm making an android app that, when
’m observing a strange behavior from my application which I hope you can explain

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.