Reputation: 35
Below code is my function in JavaScript I want confirm message after clicking delete button.
JavaScript
function deleteValues()
{
var regno = document.getElementById('regs_numb').value;
var patId =document.getElementById('Pat_Id').value;
if(regno ==""||isNaN(regno))
{
var el = document.createElement("div");
el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
el.innerHTML = "please enter register number";
setTimeout(function(){
el.parentNode.removeChild(el);
},2000);
document.body.appendChild(el);
return false;
}
else if(patId =="")
{
var el1 = document.createElement("div");
el1.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
el1.innerHTML = "please enter patient id ";
setTimeout(function(){
el1.parentNode.removeChild(el1);
},2000);
document.body.appendChild(el1);
return false;
}
else
{
document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
document.forms[0].submit();
return true;
}
}
Upvotes: 0
Views: 202
Reputation: 6170
instead of
else
{
document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
document.forms[0].submit();
return true;
}
have something like
else
{
var isSure = confirm('Are you sure you want to delete ' + patId + '?');
if (isSure) {
document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
document.forms[0].submit();
return true;
}
}
this will pop up a confirmation message with Ok and Cancel buttons. The form will submit only if the user clicks on Ok
EDIT: You might also have a mistake here
document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
// ------------------------------------------------------------------------^ You have 2 "&" symbols
Upvotes: 0
Reputation: 565
Try window.confirm() method.
Example
var r = confirm("Delete?");
if (r == true)
{
//code to delete
}
else
{
//cancel
}
Read more at http://www.w3schools.com/jsref/met_win_confirm.asp
Upvotes: 0
Reputation: 5947
This is a simple solution
function ConfirmDelete()
{
var x = confirm("Are you sure you want to delete?");
if (x)
return true;
else
return false;
}
<input type="button" Onclick="ConfirmDelete()">
Upvotes: 1
Reputation: 123
I would check out Javascript's build in confirm
method.
var s = confirm("Are you sure you want to do this?");
if(s){
// Proceed
}else{
// Uh oh, go back!
}
See here for more information: https://developer.mozilla.org/en-US/docs/Web/API/Window.confirm
Upvotes: 1