0

I have a pop up dialog box and I am trying to make it as dynamic as I can so I can send to it any function I want. I wanted to create a callback function so I can pass any function I want and it will do whatever I need on my object (in this example just print something for testing it.

here is what happens when the pop up is being called:

function DisplayPopUp(obj, callback) {


    //On Action Clicked
    $('#actionButton').click(function(e){
       e.preventDefault();
       callback(Obj);
    });
}

And here is the function that activates the PopUp function

$('.delete').click(function(){
    var obj="something something";
    DisplayPopUp(obj,function(){console.log('going to delete');});
});

Somehow that doesn't work and I get from firebug this error:

Obj is not defined

Clearly I do not transfer my function right - how should I do it?

2
  • Is it a typo that you mix Obj capitalized and lowercase? Commented Dec 18, 2011 at 16:36
  • 1
    Actual upvotes for a capitalisation typo, and downvotes for this. Silly site! Commented Dec 18, 2011 at 16:37

4 Answers 4

2

You're calling callback(Obj) but your variable name is obj. Check your case.

Sign up to request clarification or add additional context in comments.

Comments

1

When you invoke the callback in DisplayPopup(), pass the parameter obj, not Obj.

Comments

1

Well, Obj is not defined. It should be obj.

The first thing you should do when you get an error message is to read it. :)

Comments

1

I see multiple possible issues with this code:

First, the case of Obj is wrong. It should be this:

function DisplayPopUp(obj, callback) {

    //On Action Clicked
    $('#actionButton').click(function(e){
       e.preventDefault();
       callback(obj);
    });
}

But, second I don't think you're using event handlers properly. You're calling DisplayPopup from a click handler. But, in that click handler, you're installing another click handler on another object. Is that really what you want to do? Unless you've left a bunch of code out of here that creates/destroys actionButton or unbinds click handlers, you can easily end up with multiple click handlers on #actionButton each time a delete button is clicked.

8 Comments

The handlers are on different elements... If I had to guess, I'd say he's implementing an "are you sure?" box.
I know they are on different elements. But assuming that '.delete' can be clicked more than once, they will end up with multiple click handlers on #actionButton as a new handler is attached everytime it's called. It's possible that this is handled by the rest of the code, but since we can't see that, I thought I should mention it as a possible issue.
I'm going to go ahead and assume that #actionButton is created in the gap in DisplayPopUp.
@TomalakGeret'kal - As you say, that's just an assumption.
You could also call it an educated guess ;)
|

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.