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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:30:59+00:00 2026-05-25T11:30:59+00:00

I have a potentially strange question about this and jQuery plugins As I understand

  • 0

I have a potentially strange question about this and jQuery plugins

As I understand it, the following is a very basic jQuery plugin:

$.fn.clickclone = function(param){
    return this.click(function(){  
        param.apply(this);  
    });
}; 

(pretending that’s a plugin that somehow extends click().)

So, if I pass a function as an argument, it does what it needs to do and properly accesses this as a DOM node. Easy.

That’s all clear to me.

What’s not clear is, is there any way I could pass a non-function argument to the plugin and have it properly access this from the arguments? ie, could I configure the plugin to do something like this:

$("#foo").pluginname(["foo", $(this).text() ]);

Such that for:

<a href="/bar" id="foo">Bar</a>

It would properly pass an array to the plugin, with the second item in the array returning the value Bar?

I’m doing this, basically, to provide syntactic sugar for my plugin, where you can pass an array as a shortcut (in addition to using a normal callback function as the main functionality). Except, doing that, I lose access to use of this. Hence my dilemma.

EDIT: This is evil, but, it seems like one work around is to pass the argument as a string and then eval it. Not a workable solution for me, but, it illustrates what I’d like to be able to do:

$.fn.clickclone = function(param){
    return this.click(function(){ 
        if(typeof param === "function"){ 
            param.apply(this);
        }
        else if(typeof param[1] === "string"){
             console.dir("This is evil: " + eval(param[1]));
        }  
    });
}; 
  • 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-25T11:31:00+00:00Added an answer on May 25, 2026 at 11:31 am

    There’s no general way to do this without a function, since, in the purely mathematical sense, you are asking for a function of the input (that is, a function of this): something that depends on this in a certain way.

    You could perhaps hack it with strings, like so, but you lose the flexibility of functions:

    $.fn.alertMe = function (methodToAlert) {
        alert(this[methodToAlert]());
    };
    
    // usage:
    $("#foo").alertMe("text");
    $("#foo").alertMe("width");
    

    And if you find using a function acceptable but the this syntax confusing, you can simply do the following:

    $.fn.alertMe = function (alertGetter) {
        alert(alertGetter($(this));
    };
    
    // usage:
    $("#foo").alertMe(function (x$) { return x$.text(); });
    $("#foo").alertMe(function (x$) { return x$.width(); });
    

    And for completeness I guess I should mention you could probably get away with an eval-based solution, looking something like $("#foo").alertMe("$(this).text()"), but eval is evil and I will neither write up nor condone such a solution. EDIT: oh, I see you have done so in an edit to your original post. Good job corrupting future generations 😉

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

Sidebar

Related Questions

I have a very basic question about MVC web applications in Java. Since the
I have a (potentially dumb) question about the C++ STL. When I make a
I have a potentially stupid question about a potentially stupid database design. Say I
Preface: This is not much about how to structure code within files. I have
I'm about to make a design decision that could potentially have visible performance implications.
I have a potentially very long scrolling log of messages within a UITableView The
I have the following Rails link generating code (I have removed potentially 'industry secret'
Very strange error, i have a field on an MVC View: <%: Html.CheckBox(Reportable, ViewData[Reportable])%>
This is a bit of a strange question, but I thought people here might
I have the following code in my iPhone application, warning memory leak! This is

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.