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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:33:57+00:00 2026-05-23T23:33:57+00:00

I want to add a callback to a jquery widget. As an example, I

  • 0

I want to add a callback to a jquery widget.

As an example, I see that callback in the draggable widget are all wrapped in the following:

$.ui.plugin.add("draggable", "connectToSortable", {
// callbacks in here
})

Does that mean I must also wrap my callbacks in this $.ui.plugin.add({}); bit? Is there another way to do it? Like, could I have a function in the widget options that could handle this so calling my grid would look vaguely like:

var foo = {
    LoadIt: function(url, formid){
        var bar = '',
        $('#baz').grid({
            title: {somevar : true},
            rowcontent: {data: setup and populate rows},
            onComplete: function(){
                //mycallback could go here
            }
        });
    }
}, // another grid loader, etc.

In my case I am using a grid. The grid loads some json data via an ajax call and then, now that the dom is populated with the grid, I want to do some manipulation with it (add a background color on hover, for instance). So I imagine being able to call as part of the grid:

onComplete : function(){//add my background color on hover here};

Any tips or suggestions on how to approach adding a callback to a jquery widget?

An example I found that confuses me:

var Green5 = {
    setLevel: function(x){
        //...
        this.element.css({background: greenlevels[level]});
        var callback = this.options.change;
        if ($.isFunction(callback)) callback(level);
    },
    // ... rest of widget definition
};
$.widget("ui.green5", Green5);

$('.target').green5({change: function(x) { alert ("The color changed to "+x); } });

Found this on a site explaining how to add a callback to a jquery widget but I don’t see anything about the $.ui.plugin.add bit nor do I see how change is getting passed into setLevel. How does setLevel get the function that is in change? If it is simply that anything passed to green5 is an option and thus is accessible via this.options then where does the callback method that is calling level in if ($.isFunction(callback)) callback(level); come from? I’m so confused. 🙁

  • 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-23T23:33:59+00:00Added an answer on May 23, 2026 at 11:33 pm

    In the example (found here), the author points out that we can pass the callback just like we pass any other option. It may be clearer if we rewrite how he calls the widget like this:

    var options = {
        change: function(x){ alert("The color changed to "+x);}
    }
    $('.target').green5(options);
    

    What that does is bind the “change” option with an anonymous function that takes a variable and alerts the user that the color has changed to that variable. Thus, the call

    var callback = this.options.change;
    

    retrieves the anonymous function passed as the ‘change’ option and assigns it to the variable callback. The line

    if ($.isFunction(callback)) callback(level);
    

    simply checks to see if the callback is indeed a function. It’s checking for a null value / an improperly passed argument. If the callback passed in options.change is actually a function, we call it with the argument “level.”

    ui.plugin.add() does not need to be used unless we are modifying a plugin from outside of it (see example here).

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

Sidebar

Related Questions

I have some jQuery code that runs fine until I add the jQuery UI
I would like to add this to a projet, but I don't want to
Say you have a page callback from Module #1 using hook_menu(). You want to
I know this can be done easily using jQuery but haven't been able to
I've got a problem. I want to select an element inside a frame (not
I have a form that is validated once the user blurs out of an
I defined an EditText-field and I want to be informed when the user edits
The classical programmer in me is in love with the public/private paradigm of OO
I'm trying to make Vim run the command 'CommandTFlush' whenever a new file is
I'm using sqlalchemy. The question is simple, but I have a big problem, because

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.