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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:54:59+00:00 2026-06-10T22:54:59+00:00

Consider this HTML: <p>Click me</p> I would like to write an object with a

  • 0

Consider this HTML:

<p>Click me</p>

I would like to write an object with a click handler for each p, but run into the problem that my click handler function could not be found.

This code does not work:

(function(){
    var self = this;

    $("p").each(function(){
        $(this).click(self.myFunction);
    });

    self.myFunction = function(){ alert("myFunction"); }
})();​

When I wrap the click handler assignment in a function it does work:

(function(){
    var self = this;

    $("p").each(function(){
        $(this).click(function(){ self.myFunction(); });
    });

    self.myFunction = function(){ alert("myFunction"); }
})();

Question: Why doesn’t $(this).click(self.myFunction) work, but $(this).click(function(){ self.myFunction(); }) does?


Edit: The following code does work:

$("p").click(myFunction);
function myFunction(){ alert("myFunction"); }

Shouldn’t this fail as well?


P.S. I got my object working without the need to wrap the function by moving the location of the function:

(function(){
    var self = this;

    self.myFunction = function(){ alert("myFunction"); }

    $("p").each(function(){
        $(this).click(self.myFunction);
    });
})();

I guess the problem is parser related.

  • 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-10T22:55:01+00:00Added an answer on June 10, 2026 at 10:55 pm

    In this:

    (function(){
        var self = this;
    
        $("p").each(function(){
            $(this).click(self.myFunction);
        });
    
        self.myFunction = function(){ alert("myFunction"); }
    })();?
    

    The reference is evaluated immediately, and since nothing has been placed in self yet, it fails.

    Here:

    (function(){
        var self = this;
    
        $("p").each(function(){
            $(this).click(function(){ self.myFunction(); });
        });
    
        self.myFunction = function(){ alert("myFunction"); }
    })();
    

    The reference is evaluated when the click actually happens, much after self.myfunction is assigned to.

    This is in fact not a problem, and an intentional design feature.

    In this example:

    $("p").click(myFunction);
    function myFunction(){ alert("myFunction"); }
    

    The function name is being hoisted to the top of the scope. Since there is nothing dynamic about this function definition (meaning it’s not being assigned to an object dynamically), it’s available all throughout this scope, from top to bottom.

    Functions fill many gaps in JavaScript.

    They act as lambdas, modules, methods, functions and probably more that I can’t think of right now.

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

Sidebar

Related Questions

I'm parsing JSON that is contained in an HTML element. Consider this markup: <div
This problem is only in IE. Consider the following HTML: <html> <body> <div style='position:absolute;left:1em;right:1em;top:1em;bottom:1em;overflow:auto;>
Consider this small snippet of JavaScript: for(var i in map.maps) { buttons.push($(<button>).html(i).click(function() { alert(i);
Please, consider this code: <script> $(function(){ $('#clickme').click(function(){ $('#note_pag').html('<div id=note_pag><ul><li><b>1</b></li><li><a href=2>2</a></li></ul></div>'); }); $('#note_pag a').click(function(){ alert($(this).attr('href'));
I just run into the weirdest thing I've ever encounter. Consider this test page:
Question: I'm having a little trouble with jQuery: Consider this HTML, where I want
Consider this page @ http://www.bloodbone.ws/screwed.html I need to be able to have the a.grow
Consider the following HTML string: <p>This is line 1 <br /> and this is
Consider this sample html <div class=A> <div class=B> <span class=C>Sample text</span> <div class=D> <div
This is the problem page I'm developing. Consider the leftmost column header, with header

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.