This question already has answers here:
Is there a way to capture the alert ok button click event?
(6 answers)
Closed 8 years ago.
My question is: is there a way to target an element in the alert box, specifically the 'ok' button or which would stimulate the 'enter' key press. I have tried doing this code on the console without any success:
var e = $.Event("keydown", { keyCode: 13});
$("alert").trigger(e);
Popups such as alert halt execution of JavaScript, so you can't listen for an event on them. Better to use a modal popup such as Bootstrap's.
Related
This question already has answers here:
Prompt User before browser close?
(4 answers)
Closed 9 months ago.
I want to show a dialog when users click the X button to close the browser's window. In the dialog, it will ask users if they want to proceed with closing or not, if users choose no, it won't close the window.
I am wondering how to achieve this. I tried the unload event handler, the code was triggered but the window has already been closed. I also tried onbeforeunload, but it seems not triggered at all.
window.addEventListener('onbeforeunload ', () => {
// code not triggered here
});
window.addEventListener('unload', () => {
// code triggered but window is already closed
});
Even if we assume that there is an event handler which will be triggered before the window is closed, then what code should I write in order to prevent closing the window? It looks like once X button is clicked, the window is just "determined" to close and it's not reversible?
[https://stackoverflow.com/questions/2923139/prompt-user-before-browser-close]
this is the basic way to do
but prompt will be always the same
window.onbeforeunload = function(evt) {
return true;
}
This question already has answers here:
How to trigger event in JavaScript?
(19 answers)
Closed 3 years ago.
For example, I want to set inputbox value in Bing Microsoft Translator
, I can use:
$('#tta_input').val('something')
But translation event are not triggered, it just triggered when I use keyboard input something, so there is any way to trigger translation event when I use $('#tta_input').val('something') to change inputbox text?
So, you need to trigger the keypress event when the text was changed.
Try the following things -
$("#tta_input").on("change paste keyup", function() {
var e = $.Event("keypress", {which: 13});
$(this).trigger(e);
});
To check the code:
$('#tta_input').on('keypress', function(e){
console.log(e.which+' -- KeyPress event Fired');
});
You need to trigger that function on setting value by $('#tta_input').val('something') which you are using for translation on keyboard event.
E.G
$("#tta_input").on("keyup", function(e){
// Your logic of translate
});
And when you setting value by .val() then you should do this
$("#tta_input").val("xyz").trigger('keyup');
This question already has answers here:
In JavaScript can I make a "click" event fire programmatically for a file input element?
(29 answers)
Closed 9 years ago.
I get the input file element, and try to click on it in js on Chrome console, but it does not work and the file upload dialog does not appear. How can I achieve this?
document.getElementById("input_file").click();
try with onClick:
document.getElementById("input_file").onclick = function(){
//code for the action when clicked
};
Look here: http://jsfiddle.net/ghP2Y/1/
This question already has answers here:
jQuery form submit: Any way to know what element triggered the submit?
(4 answers)
Closed 8 years ago.
How to get Element(button/input) Submitting the form in Form Submit event?
You can't. Instead you should bind an event handler to all :submit elements within the form, and do your work from there.
$(document).on('click', ':submit', function () {
// this is the element that was clicked!
});
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Handling key-press events (F1-F12) using JavaScript and jQuery, cross-browser
I am working to replace an old vb6 app with a web app. in the old app the save button was linked to f8 and the users of this application want that to stay the same. How can i capture the f8 button so that it is linked to my save button? Thanks
YOu should be able to bind to the 'keyup' event and look at the keyCode. Here is a list of the keycodes you will need.
http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/javascript-char-codes-key-codes.aspx
DOM_VK_F8 = 119, so you should check to see that the keypress event listener's event object's keyCode property is equal to 119.
addEventListener('keypress', function(e) {
if (e.keyCode == 119)
; // do stuff here
}, false);