How to Login with Enter Keypress JQuery - javascript

I am trying to login with the below code when pressing Enter Keyboard button. The form is placed in the body tag with class loginpg. After pressing enter I am getting alert message & something is happening but I am not logging into pages in sales force platform. What am I missing?
$(document).keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
$('.loginpg form').submit();
return false;
}
});

Your login button must be a type="submit".
And the <input> button must be inside <form></form>.

<script>
$(document).keypress(function(e) {
if(e.which == 13) {
alert('You pressed enter!');
$('.loginpg form').submit();
}
});
</script>
I think you can try without return false;
return false; is stopping page redirection.

Related

Capture keyDown for current page only

I have forms on different pages of my applications. Upon pressing 'Enter' or 'Esc', the form on the current page must be 'Submitted' or 'Cancelled'. The keydown() function should be triggered anywhere on the page and not tied to a specific DOM element.
.js
$(document).keydown(function(e) {
if(e.which == 13) {
// enter pressed
$('#submitCreateAccountForm').click();
$('#submitForm').click();
$('#submitNewSubmissionForm').click();
}
if(e.which == 27) {
// esc pressed
$('#submitCreateAccountFormCancel').click();
$('#submitFormCancel').click();
$('#submitNewSubmissionFormCancel').click();
}
});
What should 'document' be replaced by? Thanks
try this
$(function(){
$('html').bind('keypress', function (e) {
if (e.keyCode == 13) {
//do somethings
}
else if (e.keyCode == 27) {
return false;
}
});
});
You can try this code:
$("body").keyup(function(event){ // bind keyup event to body
if(event.keyCode == 13){ // 13 - code of enter key (find for ESC)
$("#enter").click(); // bind enter press for clicking botton with id="enter" and corresponding actions
}
});

Textarea enter keypress not working because of form submit enter prevention

I have a form in which I've used the following code to prevent the form being submitted on the press of 'Enter'.
<script>
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
</script>
As a result, the 'Enter' key is not working in any textarea input. I can't enter a new line because of the body function. How do I solve this?
<textarea name='description' placeholder="Any other information (optional)"</textarea>
I have find solution.
You prevent enter key on all the form element. Just add some tweak to your code and its done. Just skip prevention of enter key when your focus is on textarea. See below code :
$(document).ready(function() {
$(window).keydown(function(event){
if(event.target.tagName != 'TEXTAREA') {
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
}
});
});
To prevent the form from submitting, try this instead:
$("form").submit(function(e){
e.preventDefault();
}

How to prevent enter key from submitting a form but still allow enter to work? [duplicate]

This question already has answers here:
Prevent users from submitting a form by hitting Enter
(36 answers)
Closed 6 years ago.
I'm using XOXCO Tags-Input.
Here is a demo http://xoxco.com/projects/code/tagsinput/example.html
So in order to complete a tag i have to hit enter but when i hit enter it submits my form.
I can disable enter for the whole form with this code but then i can't complete a tag.
$("#form").keypress(function(e) {
//Enter key
if (e.which == 13) {
return false;
}
});
So i still need enter to work but i also need it to not submit the form and i'm not sure how to do that.
You need to prevent the default behavior of the enter press, which is to submit the form. For example...
$('#form').keypress(function(e){
e.preventDefault(); // prevent form submission
// do stuff with the keypress
$('#form').submit(); // now, submit the form
});
The first thing that comes to mind is:
$('form').on('keypress keydown keyup', function(e) {
if (e.which == 13) {
//alert(e.which);
e.preventDefault();
return false;
}
});
https://jsfiddle.net/Lhujkub7/
Ok, i solved it in case anyone else runs into this problem here is what i did.
$('form input').keydown(function (e) {
if (e.keyCode == 13) {
var inputs = $(this).parents("form").eq(0).find(":input");
if (inputs[inputs.index(this) + 1] != null) {
inputs[inputs.index(this) + 1].focus();
}
e.preventDefault();
return false;
}
});

jQuery disabling submit-by-press on keyboard

I want to disable the "Enter" button on the keyboard so that when the user press enter to submit the form, nothing happens, and doing something else rather than submitting the form, such as alerting "Using keyboard is not allowed."
Here is what I have done, #calculator is a button:
$(document).ready(function(){
$("#calculator").keydown(function(){
console.log("Enter is disabled.");
return false;
});
});
Currently on its submission the form results unexpectedly (for instance redirects to the target page but without any CSS loaded.
$(document).on('keypress', function(e){
if(e.which == 13) {
e.preventDefault();
}
});
You can use .keypress() event to check which key was pressed then check the code of the key using e.keycode or e.which, if it's 13 then prevent submitting form:
$(document).keypress(function(e){
var code = e.keyCode || e.which;
if (code == 13 ) {
e.preventDefault();
return false;
}
});
For it, don't use submit button.
Use a div, style it like a button and submit the form using javascript on click of the div :)
Try this
For Disabling Keyboard
document.onkeydown = function (e) {
alert('Using keyboard is not allowed')
}
For Disabling Enter
$(document).on('keypress', function(e){
if(e.which == 13) {
e.preventDefault();
}
});

Prevent enter button form submission if email input is on focus (jquery)

I have some forms set up so that they are submitted by clicking the enter key. The problem is that some people tab to fields and then use their arrow keys and enter to select an item from their form input history.
So currently I have:
$(document).keyup(function (event) {
if (event.keyCode == 13) {
if ($("#signin").is(":visible")) {
document.getElementById('LoginAction').click();// clicks the login button
} else if ($("#createaccount").is(":visible")) {
document.getElementById('newUserAction').click();// clicks the new user button
}
}
});
and I want to make sure that if they are focused on an input that it doesn't launch the page until they are on blur to all the inputs.
Only override it for the email field:
$("#emailField").keypress(function (event) {
if (event.keyCode == 13) {
event.preventDefault();
}
});

Categories

Resources