I have a Save/Edit button. I would like it to do different things depending on whether it's currently a Save button or an Edit button. For clarity, I want to save the jQuery selector and function to a variable name, then call it:
$('div#' + id + ' .edit').click(function(){
var i = this;
var state = function(st){
if( typeof st !== 'undefined')
$(i).html(st);
else
return $(i).html();
};
if( state() == 'Edit' ) {
controller.edit(id);
state('Save');
} else {
controller.save();
state('Edit');
}
});
My question: is there a faster way to do this? Something like the following:
$('div#' + id + ' .edit').click(function(){
var state = $(this).html;
if( state() == 'Edit' ) {
controller.edit(id);
state('Save');
} else {
controller.save();
state('Edit');
}
});
...but this does not work.
This example is just an example; I know there are many ways to do what it's trying to do without saving the selector and the function to a variable. Nonetheless, the question still stands: is it possible to do this faster?