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 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 do something like this: $(#id1).html(data); I refill a div with html but when
The issue I am having is when I try to do something like the
when I try the following: if(myFunction() == 3) { alert('its three!'); } my function
Javascript (ECMAscript) supports the Array.prototype.forEach method since version 1.6 (ECMAscript edition 3, 2005). So
Say I wanted to make the following re-usable: function replace_foo(target, replacement) { return target.replace(string_to_replace,replacement);
I've hit this problem a few times, and I've always just found a work
We have built an internal tool that generates the whole data access, each table
currently I am writing an Plugin for the Internet Explorer in C#. It is
I'm using django and have a page of search results that I want to

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.