I modified my answer after your additional note.
What about..
HTML
<div class="modify askState">
<div class="ask">
<a href="#" class="change" id="1">Change</a><br>
<a href="#" class="delete" id="1">Delete</a>
</div>
<div class="confirm">
<span class="confirmDeleteText">Are you sure?</span><br/>
<a href="#" class="confirm confirmYes">Yes</a>
<a href="#" class="confirm confirmNo">No</a>
</div>
</div>
JS
var askState = true, modifyElement = $(".modify");
$(".delete").click(function(){
askConfirmation();
});
function askConfirmation() {
toggleState();
}
$(".confirmYes").click(function() {
var parent_element = $(this).closest('li');
var url_string = $(this).attr('id') + '/delete/';
$.ajax({
type: 'POST',
url: url_string,
success: function(response) {
parent_element.fadeOut(600, function() {
parent_element.remove();
});
}
});
});
$(".confirmNo").click(function() {
toggleState();
});
function toggleState() {
if( askState ) {
modifyElement. addClass("confirmState").removeClass("askState");
} else {
modifyElement.removeClass("confirmState"). addClass("askState");
}
askState = !askState;
}
CSS
.confirmState .ask {
display: none;
}
.askState .confirm {
display: none;
}
I added HTML so that the original text wont get lost (if you do innerHTML=newText
then you cannot restore its innerHTML properly without backing up).
Please also note that its fairly readable:
- IF I click "delete", then ASK confirmation (semantic code).
- IF I ask confirmation, then CHANGE State (functional code, could have been nice to make
toConfirmState()
in stead oftoggleState()
). - (then in new state)
IF I click "confirmYes", then DO perform code (functional code, could have been nice to make a delete function (semantic code))