0

I am very new to jQuery world, sorry if this is too basic a question. I have a jQuery function to create button dynamically:

function createButton(element, id, caption, leftPosition) {
    if (element == null || element.length < 1) return;

    var buttonTable = $("<table id='" + id + "'>");    
    buttonTable.addClass("togglebtn");
    buttonTable.css("left", left);

    var trow = $("<tr>");
    $("<td>")
        .text(cellText)
        .data("buttonname", caption)
        .click(function () {
            //alert("Clicked Col=" + $(this).data("buttonname"));
            TestButtonClick();
        })
        .appendTo(trow);
    trow.appendTo(buttonTable);
    buttonTable.appendTo(element);
}

Since this is a general method to create buttons, I want to add a click event to it so that the calling program can execute it's own method on click event. I mean I want to pass the client method name as parameter to createButton method which should eventually execute it on click. How is it possible to do and how to call the createButton method with that parameter?

1
  • You could try var btn = $('<button/>') Commented Oct 10, 2011 at 15:10

2 Answers 2

3

Sorry it was really a very stupid of me to post this question without trying enough. I got the answer. Here is what I did:

function createButton(element, id, caption, leftPosition, callbackfunction) {
//do something
callbackfunction();
}

Calling program:

createButton($("#tbl"), "btnSave", "Save", "100px", function () { TestButtonClick() });
Sign up to request clarification or add additional context in comments.

5 Comments

FWIW, you can also pass in a function directly, like this: createButton(…, …, …, …, TestButtonClick);
Then how to call it within the function? Should I just say TestButtonClick();
Nope. Passing in TestButtonClick works exactly the same way as passing in function(){ TestButtonClick(); }! They’re both functions, but in the second case you’re passing in a function literal, and in the first case you’re passing in a function from the variable TestButtonClick.
(So, you can just do callbackfunction() in both cases.)
One final bit of clarification… remember, named functions in JavaScript aren't magic — they’re just variables that happen to contain functions. So, passing a function, by name, as an argument to another function, is just like passing any other variable to a function.
1

Looks like I read a little too far into your question. I thought you also wanted to pass a parameter to the event handler. My answer still works, it just does a little bit extra.


You can use the call method to call another method with certain parameters. You can also pass a function into another function as a parameter. Something like this:

function createButton(element, id, caption, leftPosition, clickHandler) {
    // your stuff
    .click(function() {
        clickHandler.call(this, yourParam);
    })
    // the rest of your stuff
}

function onClick(aParam) {
    console.log(this);
    console.log(aParam);
}

createButton($("<element/>"), "id", "caption", "leftPos", onClick);

Here's a fiddle for an example.

1 Comment

thanks for you answer and it is really helpful. I meant passing the function as parameter. But this is really a good stuff to remember. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.