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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:33:31+00:00 2026-06-06T15:33:31+00:00

Hi I am learning JQuery and I have written a small function where I

  • 0

Hi I am learning JQuery and I have written a small function where I am attaching a function with a button’s click event.

this is the head element of the HTML

<script type="text/javascript">

    $(pageLoaded); 

    function pageLoaded() 
    {
        $("#Button1").bind("click", 
                            { key1: "value1", key2: "value2" }, 
                            function buttonClick(event) 
                            {
                               $("#displayArea").text(event.data.key1);
                            }
                            );
    }                    

</script>

This is the body of the HTML

<input id="Button1" type="button" value="button" />

<div id = "displayArea" style="border:2px solid black; width:300px; height:200px">

This code works fine. But when I try to write the buttonClick function outside the anonymus method, it does not work anymore.

I tried to call it this way:

$("#Button1").bind("click", 
                   { key1: "value1", key2: "value2" }, 
                   buttonClick(event));

function buttonClick(var event) 
{
      $("#displayArea").text(event.data.key1);
}

This is not working. Am I doing some mistake while passing the Event as parameter? what is the correct way to do it without using anonymous methods?

  • 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-06T15:33:33+00:00Added an answer on June 6, 2026 at 3:33 pm

    You need to pass a function reference as the click handler, but what you are doing here

    $("#Button1").bind("click", 
                       { key1: "value1", key2: "value2" }, 
                       buttonClick(event));
    

    is calling buttonClick(event) immediately and which return undefined and sets that as the click handler. You have to pass a function reference like buttonClick and you will get event param automatically (jQuery will send that when calling the handler).

    Full Code:

    $(function(){
       $("#Button1").bind("click", 
                       { key1: "value1", key2: "value2" }, 
                       buttonClick);
    
       function buttonClick(event) 
        {
          $("#displayArea").text(event.data.key1);
        } ​
    });
    

    Demo: http://jsfiddle.net/joycse06/cjc9r/


    Update (Based On @Tadeck’s comment):

    Your code will work fine if you use function expression like this

    var buttonClick = function(event){
        $("#displayArea").text(event.data.key1);
    };
    

    And you have to place this above its first use. In this case before

    $("#Button1").bind("click", ...
    

    Because function expressions are not hoisted at the top of current scope like function declaration. So you can use them only after the expression has been interpreted by JS interpreter.

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

Sidebar

Related Questions

I have a jquery function(This is not written by me anyway still I am
so I have this jQuery script below that works. But since I'm just learning
I'm learning javascript and jquery and have written a very basic script inside my
I'm learning jQuery and have a question on how to hide the submit button
I'm learning JQuery and have found the factory function $() with its selectors quite
I am new to Jquery and in learning phase. I have written a test
I'm learning jQuery/javascript and have a rather basic question. Why doesn't this work? Thanks
I am learning jQuery. I have the following chunk of code in an HTML
I am learning JQuery. I have a need to create a custom control. This
i have started to learning Jquery and i can't activate the resizable function. i

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.