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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:03:49+00:00 2026-06-19T01:03:49+00:00

in writing an addon how do you pass an event based, optional function. I

  • 0

in writing an addon how do you pass an event based, optional function. I can do it by writing a name of the function manually as a string in the calling procedure, then adding it in the this.html() as an onclick=”” in the string but that is naff. I would like to maturely pass the function as an optional argument and optionally add it as a click conditional event

the document

<div id="grid"></div>
<script>    
$(function() {
    $("#grid").myaddon({
        "headings" : [
            {
                "title" : "hello",
                "click_callback" : function(){
                    alert('hi');
                }
            },
            {
                "title" : "there"
            }
        ]
    });
});
</script>

and the addon itself

(function( $ ) {

    var methods = {
        init : function( options ) { 

            var defaults = {
                "title": "text",
                "click_callback": function() {}
            }
            for(var i=0; i<options.headings.length; i++) {
                var heading =  $.extend({},defaults,options.headings[i]);
                this.html("<div id=\"addon"+(parseInt(i))+"\" >" + heading.title + "</div>");
                if (typeof heading.click_callback == 'function') {
                    $("#addon"+(parseInt(i))).click( function() { heading.click_callback.call(this) }); //this doesn't work
                    $("#addon"+(parseInt(i))).click( function() { heading.click_callback.call() });     //this doesn't work
                    $("#addon"+(parseInt(i))).click( heading.click_callback.call());                    //this doesn't work
                    $("#addon"+(parseInt(i))).click( eval(heading.click_callback.call()));              //(in desperation) this doesn't work

                    //(edit)
                    $("#addon"+(parseInt(i))).click(heading.click_callback);                //(!!thanks Robert Fricke!) thanks to the answer from Robert Fricke I know this works
                    //(/edit)
                }
            }               
        }
    }
    $.fn.myaddon = function( method ) {

        // Method calling logic
        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } 
        else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } 
        else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.myaddon' );
        }
    };
})( jQuery );
  • 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-19T01:03:50+00:00Added an answer on June 19, 2026 at 1:03 am

    Try these:

    $("#addon"+(parseInt(i))).click(heading.click_callback); 
    $("#addon"+(parseInt(i))).click(function(){heading.click_callback();}); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Firefox addon that allows for annotating a webpage by adding
I am writing my first custom function/addon for jQuery, it's been a lot of
I'm writing a Firefox Addon and I was wondering if I could take advantage
I am writing a custom node.js addon by C++ mixed with C program. The
I'm writing a simple VisualStudio addon to attach the debugger to any IIS processes
Writing htaccess that allows me to remove index.php from the URL can confuse search
When writing filters for the Firefox Add-On 'Adblock Plus' you can write rules to
What are some resources for getting started writing a Firefox Addon? Is there an
I'm writing a simple node.js addon in C++ using Eclipse CDT. The project has
I'm writing an addon for scapy, and encountered a problem. I had to slightly

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.