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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:38:09+00:00 2026-05-18T00:38:09+00:00

I try to do something like this Javascript function as a parameter to another

  • 0

I try to do something like this Javascript function as a parameter to another function?, but unfortunately I haven’t succeeded. I’ve done something wrong, but I don’t know what. Please help me!

There is a div-element and a function. What the function does is: takes an another function as a parameter and execute it on click on the div-element:

UPDATE3:
I’ve tried to get the code to work with your examples beneath, with my original web project. But I’ve got some problem with some parameters. I hope you can answer this question as fast as the others.

UPDATE4:
Thanks Andy E! and all other who have helped me! Really appreciate it!

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>

    
  </head>
  <body>
    <div id='divElement'>Hello World!</div>
    <script type="text/javascript">
        
        function button(exefunction){
            //Some code that decide which date
            var date = '20101010';
            document.getElementById('divElement').onclick = exefunction(date);
        }
        function testfunction(text){
            document.getElementById('divElement').innerHTML = text;
        }
        button(testfunction);
    </script>
  </body>
</html>
  • 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-18T00:38:09+00:00Added an answer on May 18, 2026 at 12:38 am

    change testfunction to function and you’ll have an anonymous function that is passed as the argument. You also need to remove the parens from exefunction (with them the function is called instead of assigned):

        function button(exefunction){
            document.getElementById('divElement').onclick = exefunction;
        }
        button(function(){
            document.getElementById('divElement').innerHTML = 'Changed';
        });
    

    Here is a working example to play around with.


    Remove your parens!

        button(testfunction);
        // not button(testfunction());
    

    The brackets execute the function instead of passing it as an argument. When you execute a function its return value is passed instead.

    Second working demo.


    For your last edit, you need to create another anonymous function for the code to work:

            document.getElementById('divElement').onclick = function () {
                exefunction(date);
            }
    

    Third (final?) working demo.

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

Sidebar

Related Questions

I would try something like this but it isn't allowed. function GetDynamicModulesProperties() { var
Using something like this: try: # Something... except Exception as excep: logger = logging.getLogger(component)
Right now we just use something like this stopWatch.Start(); try { method(); } finally
When I try to use curl or file_get_contents to read something like http://example.com/python/json/ from
Let's say I do something in Java like: RemoteResponse response = null; try {
Hoping that using something like this demo it is possible to drag items within
whenever I try to run something in Java, Eclipse changes into the PHP perspective.
Every time I try to do something seemingly-simple in CSS, it doesn't work. I
I am assigning ViewState[something] in Page Load of content page and when I try
I'm trying to write someting into the DB using PHP but if I try

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.