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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:31:58+00:00 2026-05-15T21:31:58+00:00

I am creating a plugin and it cannot access $(this). A simple overview of

  • 0

I am creating a plugin and it cannot access $(this). A simple overview of my plugin is

(function($){
    $.fn.myPlugin= function(options, callback) {
        return this.each(function(){
                $(this).click(function(){
                      // some plugin works ..

                      callback();
                });
        });
    };

})(jQuery);

Then i attached my plugin to a element like

$('p').myPlugin({
      // Some options
}, function(){
      alert('first test');
      alert($(this).text());
});

Here when the element p is clicked, i get the first alert, but i didn’t get the 2nd alert.

The callback function is called, but unable to access this.
Is there any problem with the defining or with code ? any alternative suggestion will also be helpful

  • 1 1 Answer
  • 3 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-15T21:31:59+00:00Added an answer on May 15, 2026 at 9:31 pm

    Instead of callback();, use .apply() to give it the right context (otherwise it’s window), like this:

    callback.apply(this);
    

    You can see the updated/working version here.


    For a more complete overview here, you can pass more arguments if you want to that callback, for example if you wanted to make the options available, you could do this:

    (function($){
      $.fn.myPlugin= function(options, callback) {
        return this.each(function(){
          $(this).click(function(){
            callback.apply(this, [options]);
         });
       });
     };
    })(jQuery);
    

    Then call it like this:

    $('p').myPlugin({
        thing: "thing1"
    }, function(opts){
        alert(opts.thing); //thing1
    });​
    

    You can give that one a try here, just add whatever arguments you want to that array, callback() will be called with those arguments 🙂

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

Sidebar

Related Questions

I came across this problem when I was creating a plugin. I cannot call(access
I'm reading some tutorials now about jQuery.. function creating,plugin creation etc. but these tutorials
I'm interested in creating a plugin that does this simple task: Steps 1. User
I have the following problem: I am creating a simple plugin jquery carousel, to
I'm creating a new plugin for TinyMce. However I cannot find any examples to
This question is in relation to a Plugin which I am now creating for
I am creating a small plugin that process some images. Now to report the
I'm having maven project on Eclipse with m2eclipse plugin. This project has some dependencies.
So I'm creating a plugin for Ruby on Rails to make implemented addresses including
I'm creating a plugin for wordpress and I need to check if a post

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.