I have posted on this thread - How do I pass function as a parameter in javascript for creating pop up box request with several functionality.
One of the answers there stated a problem that I thought should have a new thread.
My task is this - I have several buttons for each item (let's say I have several articles and each one I can delete, edit etc..) - for each button I want a pop up box that asks me if I'm sure or not sure I want to do this task.
How do I do it correctly?
this is an example for what happens when I click on of the delete buttons:
$('.delete').click(function(){
var obj={title:"The Header",someText:"Are you sure?"};
DisplayPopUp(obj,function(){console.log('going to delete');});
});
after I have the "are you sure?" pop up - this is the pop up function:
function DisplayPopUp(obj, callback) {
//On Action Clicked
$('#actionButton').click(function(e){
e.preventDefault();
callback(obj);
});
}
My main concern is that each time I click the delete button - I multiply the "are you sure" button function. How do I delete this event before I re-create it? Where do I need to put it?
and in general - is this the right method to do it or is there a better, cleaner code (assuming I need to do it without using jQuery-UI?
take to consideration that I want it to be fluid and to work on several tasks like create/delete/edit etc...
thanks, Alon
=========== THE HTML ==============
looks like this: - I use $('content').html(theContentIneed) to fill the content and toggle display modes to hide/unhide it.
<div id="popup">
<div id="content">
</div>
<a id="actionButton">action</a>
</div>
Now that we can see that you're only hiding/showing the HTML (not creating/destroying it), you can solve the issue of too many click handlers by assinging the event handler once before anything starts and use the .data() method to store your context:
Or, if you really wanted to do it the way you were doing it and are using jQuery 1.7+:
You might want to use the jQuery plugin pattern: