How to Press Enter Key Automatically Using Jquery

How to press enter key automatically using Jquery

Try keyup event instead of change event

$("#table1_filter input").val(this.value).trigger('keyup')

Jquery: how to trigger click event on pressing enter key

try out this....

$('#txtSearchProdAssign').keypress(function (e) {
var key = e.which;
if(key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});

$(function() {

$('input[name="butAssignProd"]').click(function() {
alert('Hello...!');
});

//press enter on text area..

$('#txtSearchProdAssign').keypress(function(e) {
var key = e.which;
if (key == 13) // the enter key code
{
$('input[name = butAssignProd]').click();
return false;
}
});

});
<!DOCTYPE html>
<html>

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>

<body>
<textarea id="txtSearchProdAssign"></textarea>
<input type="text" name="butAssignProd" placeholder="click here">
</body>

</html>

Press the enter key in a text box with jQuery

Use keypress then check the keycode

Try this

$('input').on('keypress', function(e) {
var code = e.keyCode || e.which;
if(code==13){
// Enter pressed... do anything here...
}
});

OR

e = jQuery.Event("keypress")
e.which = 13 //choose the one you want
$("#test").keypress(function(){
alert('keypress triggered')
}).trigger(e)

DEMO

Can't auto submit when press enter key

Your HTML code is not proper.

The issue is with < and >characters.

e.g. here,

<form action='<?php echo site_url(); ?>account_c/login' method="POST">

You have to escape them for the form to generate the URLs properly.

Check if the action url is created properly on page, I bet it won't be.
Your form is actually getting submitted but not going anywhere.

Try with,

<form action='<?php echo site_url(); ?>account_c/login' method="POST">

Similarly,

<a href="<?php echo site_url(); ?>/home/forgotpasswordmail""

should be,

<a href="<?php echo site_url(); ?>/home/forgotpasswordmail">

How can I automatically trigger an enter key press event after a certain action is executed?

  1. Create a variable that tells us if the user has clicked your button.

    var clicked = false;
  2. Add an event listener to your button, so that when the user clicks on it, the clicked variable will become true.

    myButton.addEventListener('click', function(){
    clicked = true;
    });
  3. Add a keypress event listener:

    document.addEventListener('keypress', function(e) {
    // `e` is the event
    });
  4. Inside that event listener, check if the user clicked previously

    if(clicked) {
    // ...
    }
  5. If (s)he did, check the pressed key. Enter's key code is 13.

    var keynum = e.keyCode||e.which;
    if(keynum == 13) {
    // ...
    }
  6. If the pressed key was enter, use

    clicked = false;

    Otherwise, once the user clicked your button and pressed enter, it wouldn't be necessary to click the button again.

  7. After that run your code. For example, this will call function f after 2 seconds.

    setTimeout(f, 2000);

Live demo

var clicked = false;document.querySelector('#btn').addEventListener('click', function(){  clicked = true;  snippet.log("You clicked the button. `clicked` is now `true`");});document.addEventListener('keypress', function(e) {  if(clicked) {    var keynum = e.keyCode || e.which;    if(keynum == 13) {      clicked = false;       snippet.log("`clicked` is now `false`. Waiting 2 seconds...");      setTimeout(f, 2000);    }  }});function f() {  snippet.log("Function `f` executed successfully!");}
#btn {  border: 3px solid red;  cursor: pointer;}
<div id="btn">Click me, and then press enter.</div><!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --><script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


Related Topics



Leave a reply



Submit