Confirmation Box on specific button click - javascript

I need a confirmation box to pop up when a user hits a Save button on my webpage. I currently have a confirm box showing up whenever a user hits ANY button. I need it strictly for the one. The button I need it linked to is called btnsavesurvey.
Here is my current code in the page_load:
Dim message As String = "Do you want to complete survey?"
Dim sb As New System.Text.StringBuilder()
sb.Append("return confirm('")
sb.Append(message)
sb.Append("');")
ClientScript.RegisterOnSubmitStatement(Me.GetType(), "alert", sb.ToString())

$('#btnsavesurvey').click(function(){
var r = confirm("Your message");
if (r == true) {
alert("You pressed OK!");
// Do something when the user pressed ok
} else {
alert("You pressed Cancel!");
// Do something when the user pressed cancel
};
});
If your btns id is btnsavesurvey if its class is btnsavesurvey than use .btnsavesurvey

Related

Bootstrap Modal - Simple password protected button

I have a page with a modal. Inside the modal , i have two buttons. Close and Save Changes. I am trying to add a simple password protected on Save Changes button press. User press button and a pop up with only password. If password is correct continue the rest code. Else alert message.
js:
$('#UpdateForm').submit(function(event) {
if ($pass=="pass") {
echo "Rest Code Here";
} else {
echo "Wrong!";
}
}
I think the code would like like this. How to add pop up and make it work. It is simple protected and no secure as is for local use.
You can use the Window prompt() Method.
The prompt() method displays a dialog box that prompts the visitor for input.
A prompt box is often used if you want the user to input a value before entering a page.
Note: When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value. Do not overuse this method, as it prevents the user from accessing other parts of the page until the box is closed.
w3schools.com - Window prompt() Method
You can try the following code:
$('#UpdateForm').submit(function(event) {
y = prompt("Please insert code to continue.");
if (y == "pass") {
alert("Rest Code Here");
} else {
alert("Wrong!");
//Stop closing modal
return false;
}
})
You can use SweetAlert2 to do it
See here https://codepen.io/5hiny/pen/PobNbGQ
Swal.fire({
title: 'Enter password to save',
input: 'text',
showCancelButton: true,
confirmButtonText: 'Submit',
showLoaderOnConfirm: true,
preConfirm: (pass) => {
if (pass == 'password') {
document.write("SAVE");
} else {
document.write("FAIL");
}
},
allowOutsideClick: () => !Swal.isLoading()
})

javascript confirm box shows unwanted message

I am using below code to show a confirm box. I see "this site says" in the alert. How can I remove that text? I don't want to use Jquery UI library.
var r = confirm("Press a button");
if (r == true) {
x = "You pressed OK!";
} else {
x = "You pressed Cancel!";
}

alert box disappear without clicking on ok button in asp.net

I am making in which I want to display alert box and then redirect it to some other page in asp.net
My code is
var s = Convert.ToInt32(Session["id"].ToString());
var q = (from p in db.students
where p.userid == s
select p.sid).SingleOrDefault();
if (q == 0)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "reg();", true);
}
this is my reg() function
function reg() {
var con = alert('Please Register Your Self Fisrt ..!! Thank you');
window.location = "reg.aspx";
}
My code is working fine but alert box gets disappear after some second without clicking on it because of that user is unable to read alert message
I want to redirect it to reg.aspx but after clicking OK on alert box ..
please help
Firing a confirm box instead of alert will do what you are trying to achieve. You will have the option to redirect conditionally or unconditionally
function reg(){
var con=confirm("You need to register first before proceeding. Redirect to Registration Page");
if(con==true){
window.location = "reg.aspx";
}
else{
//if redirect unconditionally
window.location = "reg.aspx";
}
}

Submit button doesn't run function in panel add-on sdk

I am developing an add on using mozilla's add-on sdk. I have a panel attached to a toggle button that contains one input text box and a submit button. When the user presses enter, I am able to get my function to work. However, when I press the submit button nothing happens. Why is this? I have attached my code below:
// When the user hits return, send the "text-entered"
// message to main.js.
// The message payload is the contents of the edit box.
var textArea = document.getElementById("numTimes");
textArea.addEventListener('keyup', function onkeyup(event) {
if (event.keyCode == 13) {
// Remove the newline.
text = textArea.value.replace(/(\r\n|\n|\r)/gm,"");
self.port.emit("text-entered", text);
textArea.value = '';
console.log("Got text from enter");
}
}, false);
// Listen for the "show" event being sent from the
// main add-on code. It means that the panel's about
// to be shown.
//
// Set the focus to the text area so the user can
// just start typing.
self.port.on("show", function onShow() {
textArea.focus();
});
var submit = document.getElementById("search");
submit.addEventListener("click", function submit(){
text = textArea.value.replace(/(\r\n|\n|\r)/gm,"");
self.port.emit("text-entered", text);
textArea.value = '';
console.log("Got text from submit button");
}, false);
The code that's within the textArea section works but the code by the submit section doesn't. Does JavaScript treat these differently?

Confirm dialog in Cordova

I'm writing an app in javascript and html with phonegap/cordova.
I have this code in javascript:
$('#diario_delete_btn').live('tap',function(e){
var iddb = $(this).find('a').attr('rel');
confirm_diario_delete(iddb);
});
function diario_delete(iddb) {
var db = window.openDatabase("Database", "1.0", "123nozze", 200000);
db.transaction(function(tx){
tx.executeSql('DELETE FROM AgendaItemJDO WHERE id='+iddb);
lastChangeUpdate();
});
$('.diario_row[db_id="'+ iddb +'"]').remove();
$('#popupMenuDiario').popup("close");
}
function confirm_diario_delete(iddb) {
var r = confirm("Confermi l'eliminazione dell'elemento?");
if (r) {
diario_delete(iddb);
} else {
$('#popupMenuDiario').popup("close");
}
}
It seems to work but if I choose "cancel button" (so r = false) n times before I press "confirm button", next time the confirm dialog is displayed 2 times, next time it is displayed 3 times, and so on. I don't know why this behaves this way. The same is if a change the code and I use the Cordova example code for confirm dialog.
Any ideas on what is the problem and how can I solve it?
Thanks!
You should use the native notification which Phonegap supports.
Specifically the .confirm() method taken from link above;
// process the confirmation dialog result
function onConfirm(button) {
alert('You selected button ' + button);
}
// Show a custom confirmation dialog
//
navigator.notification.confirm(
'You are the winner!', // message
onConfirm, // callback to invoke with index of button pressed
'Game Over', // title
'Restart,Exit' // buttonLabels
);
navigator.notification.confirm(
'Are you sure you want to signup ', // message
onConfirm, // callback to invoke with index of button pressed
'SignUp', // title
['yes','no'] // buttonLabels
);
function onConfirm(buttonIndex){
alert('You selected button ' + buttonIndex);
if(buttonIndex==2){
alert('You selected button - no');
}else{
alert('You selected button - yes');
}
}

Categories

Resources