Easiest way to create confirmation using jQuery / JavaScript?

How can i achieve this?

  • The user clicks the delete link (with the class "confirm").
  • A confirmation message appears asking, "Are you sure?" with the options "Yes" and "Cancel".

If β€œYes” is selected, the link continues by clicking, but if cancel is selected, the action is canceled.

Update: Final working code with confirm() thanks to this guy :

 $('.confirm').click(function() { return confirm("Are you sure you want to delete this?"); }); 
+7
javascript jquery
source share
3 answers

Javascript provides a built-in confirmation dialog.

 if (confirm("Are you sure?")) { // continue with delete } 
+17
source share

in my experience, this is the best and easiest way to get confirmation!

 <a href="#" onclick="return myconfirm()">Confirm</a> <script> function myconfirm() { if(confirm('Are You Sure ...')) return true; return false; } </script> 
+1
source share

I have successfully completed the confirmation window in jQuery. make sure you have the jQuery and css INCLUDED library in your code before trying to do this (jquery-ui-1.8.16.custom.css, jquery-1.6.2.js, jquery-ui-1.8.16.custom .min. JS). The main difference between JAVASCRIPT confirmation of the box and that box that we are created using the DIV - SO WHAT - CONFIRMATION JAVASCRIPT IS WAITING FOR INPUT USER after logging YES / NO, the next line is made, HERE YOU HAVE TO MAKE THAT YES, OR NO BLOCK - ** THE NEXT CODE LINE AFTER showConfirm () will be executed immediately * , so be careful

 /** add this div to your html 

* /

 var $confirm; var callBack; var iconStyle = '<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 50px 0;"></span>'; var messageStyleStart = '<span align="center" style="font-family:arial, verdana, sans-serif;font-size:9.3pt;">'; var messageStyleEnd = '</span>'; $(document).ready(function(){ $('#confirmDialog').dialog({ autoOpen: false, modal: true }); //jquery confirm box -- the general alert box $confirm = $('<div style="vertical-align:middle;"></div>') .html('This Message will be replaced!') .dialog({ autoOpen: false, modal: true, position: 'top', height:300, width: 460, modal: true, buttons: { Ok : function() { $( this ).dialog( "close" ); if(null != callBack) callBack.success(); }, Cancel: function() { $( this ).dialog( "close" ); if(null != callBack) callBack.fail(); } } }); }); function showConfirm(message,callBackMe,title){ callBack = null; $confirm.html(""); // work around $confirm.html(iconStyle + messageStyleStart +message + messageStyleEnd); if(title =='undefined'|| null ==title) $confirm.dialog( "option", "title", "Please confirm" ); else $confirm.dialog( "option", "title", title); var val = $confirm.dialog('open'); callBack = callBackMe; // prevent the default action return true; } // Now for calling the function // create a Javascript/jSOn callback object var callMeBack = { success: function() { // call your yes function here clickedYes(); return; }, fail: function (){ // call your 'no' function here clickedNo(); return ; } }; showConfirm("Do you want to Exit ?<br/>"+ ,callMeBack1,"Please Answer"); 
0
source share

All Articles