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!";
}
Related
I need the script for functioning the window.location only after pressing the ENTER key 3 or n times.
This is my current code and i am trying to modify it. Need help, still.
function KeyPress(f) {
var event = window.event? event : f
if (event.keyCode == 13)
window.location = './index.html';
}
document.onkeypress = KeyPress;
Following your instructions, after three press to the key ENTER, it should run the code that will call window.location. In this example, I'm using console.log to prove it is doing what you asking.
Note: When you run it, you need to click with the mouse where it says "Press Enter 3 times.". In this way, the browser will focus on that section. Then, you can press the ENTER key three times.
document.addEventListener("keyup", (e) => enterKeyPressed(e))
let counter = 1;
function enterKeyPressed(event) {
console.log("Key", event.keyCode, " Pressed:", counter);
if (event.keyCode == 13 && counter == 3) {
console.log("Enter key is pressed");
// window.location = "<url you want to go>";
return true;
}
counter++;
return false;
}
Press Enter 3 times.
Check the log.
I expect the behavior of the program listed below to be like this:
I click on the input.
I press enter.
The line "Press Enter once again" appears.
I go out of input by clicking somewhere on the page.
I press Enter.
Then the line "You have pressed Enter 2 times" appears.
The real behavior of this program:
I click on the input.
I press enter.
The lines "Press Enter once again" and "You have pressed Enter 2 times" appear.
I have 2 questions:
1) What is the reason for that? I have pressed Enter only once.
2) How to make this program behave the way I expect it to behave?
let input = document.createElement("input");
input.value = "Click on this field and press Enter";
input.addEventListener("keydown", function(){
if(event.keyCode === 13){
let div2 = document.createElement("div");
div2.innerHTML = "Press Enter once again";
document.addEventListener("keydown", function(){
if(event.keyCode === 13){
let div3 = document.createElement("div");
div3.innerHTML = "You have pressed Enter 2 times";
document.body.appendChild(div3);
}
});
document.body.appendChild(div2);
}
});
document.body.appendChild(input);
Let's break this down. What is happening is that your if condition is immediately checking if the key was pressed, and it has been so e.keyCode is always 13.
The easiest method to solve this is with the following bit of code:
let input = document.createElement("input");
let isPressed = false;
input.value = "Click on this field and press Enter";
document.addEventListener("keydown", function(e) {
if (event.keyCode === 13 && isPressed) {
let div3 = document.createElement("div");
div3.innerHTML = "You have pressed Enter 2 times";
document.body.appendChild(div3);
}
});
input.addEventListener("keydown", function(e){
event.stopPropagation()
if(event.keyCode === 13){
if ( isPressed ) {
return;
} else {
let div2 = document.createElement("div");
div2.innerHTML = "Press Enter once again";
isPressed = true;
document.body.appendChild(div2);
}
}
});
document.body.appendChild(input);
You can try that out here. You can refractor this to your liking. :)
Keeping your original code in mind, here is the updated version.
To understand how this bit works, it's simple: when there is an event in element a and the wrapper for that element is b, the event travels up through a process known as event bubbling (because bubbles rise up).
Due to that, the second event listener, on the document, gets executed.
To stop that from happening, you can use .stopPropagation() on the event object.
There were a couple of things going on here. You were attaching the listener twice, the second time on the whole document, this event then got triggered instantly when the event bubbled. You didn't need to use two different elements for the message either, if you keep track of if enter was pressed. You should also use textContent over innerHTML for security reasons :)
Here is the code rewritten
let enterHasBeenPressed = false;
let input = document.createElement("input");
input.value = "Click on this field and press Enter";
input.addEventListener("keydown", function(){
if(event.keyCode === 13){
onEnterPressed();
}
});
document.body.appendChild(input);
let text = document.createElement("div");
document.body.appendChild(text);
function onEnterPressed() {
if (!enterHasBeenPressed) {
text.textContent = "Press Enter once again";
enterHasBeenPressed = true;
return;
}
text.textContent = "You have pressed Enter 2 times";
}
https://codepen.io/anon/pen/wbjojR
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
I want to create a popup window in Javascript with two options 'Allow' or 'Deny'. Then I want to collect information from the option chosen and take particular action.
if (allow) { do foo } else { do bar }
How to code a popup from whose mouseclick, the value of allow or deny buttons can be fetched?
Simple..
function myFunction() {
var r = confirm("Press a button!");
if (r == true) {
alert("take as yes");
} else {
alert("take as no");
}
demo :jsfiddle
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');
}
}