Click here to Skip to main content
15,910,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create the pop window when user click check-box. When the check box is clicked, I display the pop up with relevant message for user to confirm if the really want to click the check box. Same when the check box is checked and user want to uncheck it. I want to display the pop up with relevant message if the really want to uncheck the checkbox.

My code:

js code

JavaScript
function CustomAlert(){
	this.render = function(dialog){
		var winW = window.innerWidth;
	    var winH = window.innerHeight;
		var dialogoverlay = document.getElementById('dialogoverlay');
	    var dialogbox = document.getElementById('dialogbox');
		dialogoverlay.style.display = "block";
	    dialogoverlay.style.height = winH+"px";
		dialogbox.style.left = (winW/2) - (550 * .5)+"px";
	    dialogbox.style.top = "100px";
	    dialogbox.style.display = "block";
		document.getElementById('dialogboxhead').innerHTML = "Acknowledge This Message";
	    document.getElementById('dialogboxbody').innerHTML = dialog;
		document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()">OK</button>';
	}
	this.ok = function(){
		document.getElementById('dialogbox').style.display = "none";
		document.getElementById('dialogoverlay').style.display = "none";
	}
}
var Alert = new CustomAlert();
function checkBoxPost(id){
	var db_id = id.replace("post_", "");
	$('input:checkbox').click(function(){
			var checked = $(this).is(':checked');
			if(checked) {
				if(!confirm('Are you sure you want to enable all device use?')){         
					$(this).removeAttr('checked');
				}
			} else if(!confirm('Are you sure you want to disable all device use?')){
				$(this).attr("checked", "checked");
			}
	});
}
function CheckBoxConfirm(){
	this.render = function(dialog,op,id){
		var winW = window.innerWidth;
	    var winH = window.innerHeight;
		var dialogoverlay = document.getElementById('dialogoverlay');
	    var dialogbox = document.getElementById('dialogbox');
		dialogoverlay.style.display = "block";
	    dialogoverlay.style.height = winH+"px";
		dialogbox.style.left = (winW/2) - (550 * .5)+"px";
	    dialogbox.style.top = "100px";
	    dialogbox.style.display = "block";
		
		document.getElementById('dialogboxhead').innerHTML = "Confirm that action";
	    document.getElementById('dialogboxbody').innerHTML = dialog;
		document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Confirm.yes(\''+op+'\',\''+id+'\')">Yes</button> <button onclick="Confirm.no()">Cancel</button>';
	}
	this.no = function(){
		document.getElementById('dialogbox').style.display = "none";
		document.getElementById('dialogoverlay').style.display = "none";
	}
	this.yes = function(op,id){
		if(op == "check_box"){
			checkBoxPost(id);
		}
		document.getElementById('dialogbox').style.display = "none";
		document.getElementById('dialogoverlay').style.display = "none";
	}
	
}
var Confirm = new CheckBoxConfirm();



HTML code
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/dialog.css">
<script src="js/dialog.js"></script>
</head>
<body>
<div id="dialogoverlay"></div>
<div id="dialogbox">
  <div>
    <div id="dialogboxhead"></div>
    <div id="dialogboxbody"></div>
    <div id="dialogboxfoot"></div>
  </div>
</div>
<label for="sync_payments"><input type="checkbox" id="sync_payments" name="sync_payments" onclick="Confirm.render(checkBoxPost)"/> Enable for all device(s) </label>


<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
</body>

</html>



My Style:
CSS
#dialogoverlay
{
	display: none;
	opacity: .8;
	position: fixed;
	top: 0px;
	left: 0px;
	background: #FFF;
	width: 100%;
	z-index: 10;
}
#dialogbox
{
	display: none;
	position: fixed;
	background: #99cc00  ;
	border-radius:7px; 
	width:500px;
	z-index: 10;
}
#dialogbox > div{ background:#FFF; margin:8px; }
#dialogbox > div > #dialogboxhead{ background: #C1e0ea ; font-size:19px; padding:10px; color:#fff; }
#dialogbox > div > #dialogboxbody{ background: #75A9BC; padding:20px; color:#FFF; }
#dialogbox > div > #dialogboxfoot{ background: #C1e0ea; padding:10px; text-align:right; }


What I have tried:

I have tried to call the onclick="Confirm.render('You sure you want to sync?','check_box')" on the check box but this didn't help.
Posted
Updated 5-May-16 0:24am
Comments
Karthik_Mahalingam 5-May-16 5:38am    
you mean confirm alert?
iDoKin 5-May-16 5:40am    
Yes, whaat is it I m doing wrong on my code? The is the link https://fiddle.jshell.net/9herrf9r/
Karthik_Mahalingam 5-May-16 5:50am    
your requirement is bit confusing.. pls tell step by step operation.
dont confuse us with modal popup and confirm box ..
iDoKin 5-May-16 5:57am    
1. If user clicks the checkbox, i want to show my custom pop up to ask user if they really want to check it. If user click OK, I check the checkbox. Else if they click CANCEL keep checkbox unchecked.
2. If user want to uncheck the checkbox, I want to also show my custom pop up to ask user if they really want to uncheck the checkbox. If they click OK uncheck the checkbox. Else if they click CANCEL keep checkbox checked.
Karthik_Mahalingam 5-May-16 6:24am    
verify the functionality from below code

1 solution

JavaScript
<script>
       function CustomAlert() {
           debugger;
           this.render = function (dialog) {
               debugger;
               var winW = window.innerWidth;
               var winH = window.innerHeight;
               var dialogoverlay = document.getElementById('dialogoverlay');
               var dialogbox = document.getElementById('dialogbox');
               dialogoverlay.style.display = "block";
               dialogoverlay.style.height = winH + "px";
               dialogbox.style.left = (winW / 2) - (550 * .5) + "px";
               dialogbox.style.top = "100px";
               dialogbox.style.display = "block";
               document.getElementById('dialogboxhead').innerHTML = "Acknowledge This Message";
               document.getElementById('dialogboxbody').innerHTML = dialog;
               document.getElementById('dialogboxfoot').innerHTML = '<button >OK</button>';
           }
           this.ok = function () {
               debugger;
               document.getElementById('dialogbox').style.display = "none";
               document.getElementById('dialogoverlay').style.display = "none";
           }
       }
       var Alert = new CustomAlert();
       function checkBoxPost(id) {


       }
       function CheckBoxConfirm() {
           this.render = function (dialog, op, id) {
               debugger;
               var winW = window.innerWidth;
               var winH = window.innerHeight;
               var dialogoverlay = document.getElementById('dialogoverlay');
               var dialogbox = document.getElementById('dialogbox');
               dialogoverlay.style.display = "block";
               dialogoverlay.style.height = winH + "px";
               dialogbox.style.left = (winW / 2) - (550 * .5) + "px";
               dialogbox.style.top = "100px";
               dialogbox.style.display = "block";

               if ($('#sync_payments').is(":checked"))
                   document.getElementById('dialogboxbody').innerHTML = 'Are you sure you want to enable all device use?';
               else
                   document.getElementById('dialogboxbody').innerHTML = ' Are you sure you want to disable all device use?';



               document.getElementById('dialogboxhead').innerHTML = "Confirm that action";
               // document.getElementById('dialogboxbody').innerHTML = dialog;
               document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Confirm.yes(\'' + op + '\',\'' + id + '\')">Yes</button> <button onclick="Confirm.no()">Cancel</button>';

           }
           this.no = function () {
               debugger;
               document.getElementById('dialogbox').style.display = "none";
               document.getElementById('dialogoverlay').style.display = "none";

               if ($('#sync_payments').is(":checked")) { // checked

                   $("#sync_payments").attr('checked', false);
               }
               else {
                   $("#sync_payments").attr('checked', true);
               }

           }
           this.yes = function (op, id) {
               debugger;
               //if (op == "check_box") {
               //    checkBoxPost(id);
               //}
               document.getElementById('dialogbox').style.display = "none";
               document.getElementById('dialogoverlay').style.display = "none";

               if ($('#sync_payments').is(":checked")) { // checked
                   // leave as it is
               }
               else { // unchecked
                   $("#sync_payments").attr('checked', false);
               }
           }

       }
       var Confirm = new CheckBoxConfirm();
   </script>
 
Share this answer
 
v6
Comments
iDoKin 5-May-16 8:09am    
Now it does not show the custom pop up when the checkbox is checked. Did you do something wrong on the code? I see you commented out this " // document.getElementById('dialogboxbody').innerHTML = dialog;"
Karthik_Mahalingam 5-May-16 8:33am    
its not pasting properly, body portion use your old code..
iDoKin 5-May-16 8:48am    
I did you my old code and it work like a charm. Thanks KARTHIK you the best.
Karthik_Mahalingam 5-May-16 8:50am    
At last mission completed.. i can take a coffee now :)
iDoKin 5-May-16 8:52am    
Yes mention is completed we must take a coffee to cheer to the success of mention. :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900