vendredi 3 avril 2015

Using the jquery.confirm function on change event of checkbox

I am using jquery.confirm which i picked up from http://ift.tt/XfnSma


Ojective :- I am trying to pop up a confirmation box with different text on check and uncheck of the checkbox. On check of the checkbox a confirmation box should pop up showing the message "Are You Sure to add the media", if user clicks "Yes I am", i am sending the corresponding ajax call for adding the media and making the checked property true. On uncheck of the checkbox the confirmation should pop up showing the message "Are you Sure to delete the media", again if the user selects "Yes I am", a ajax call is being made with for deletion and checked property is set to false.


The checkbox is:



<input type="checkbox" id="media">


Now the issue is although the flow is going correctly, the confirmation box is showing the same textMessage on both check and uncheck of the checkbox.



"Are You sure to add the media".


Instead it should change the text since the textMessage is changing on each check and uncheck. This is the first time i am using the confirmation box.


This is the code.



var textMessage=" to add the media?";
$('#media').change(function() {
//var checked=$(this).is(":checked");
var me=this;
$("#media").confirm({
title:"Confirmation",
text:"Are You Sure" + textMessage,
confirm: function(button) {
if(textMessage === " to add the media"){
$(me).prop("checked", true);
mediachecked=false;
textMessage=" to delete the media?";
//Making the ajax calls for addition
}
else{
$(me).prop("checked",false);
mediachecked=true;
textMessage=" to add the media?";
//Making the ajax calls for deletion
}
},
cancel: function(button) {
},
confirmButton: "Yes I am",
cancelButton: "No"
});
});


Thanks in advance for the help.


Aucun commentaire:

Enregistrer un commentaire