Javascript: Enter a keyword

Good morning...

I use a java script on each page to trigger a key Enterby clicking Event inside a text box. It is working fine. Now I want to put the code in a file .jsfor global access.

 function EnterKeyPress(id,e) {
       // look for window.event in case event isn't passed in
       if (window.event) { e = window.event; }
       if (e.keyCode == 13 || e.keyCode == 121) {

           window.document.getElementById('<%= ibtnSubmit.ClientID %>').click();
       } 
   }

I do not want to hardcode the identifier of the control. Can anyone help me please.

+3
source share
3 answers

You can use your id parameter instead of ControlID, and when you use this function on your pages, you can pass ControlID as a parameter:

function EnterKeyPress(id,e) {
  if (window.event) { e = window.event; }
   if (e.keyCode == 13 || e.keyCode == 121) {
     document.getElementById(id).click();
   }
}

You are using ASP.NET, so in your code you can assign a keypress event:

TextBox1.Attributes.Add("onkeypress",
                      string.Format("EnterPressKey('{0}', event);", ibtnSubmit.ClientID));

, ASP.NET, ASP: Panel DefaultButton , , , , Enter .

+9

jQuery, :

$("input[id*=ibtnSubmit]").click()
0

, , Enter, javascript. . :

<form action="process.php">
  <input type="text">when the enter key is pressed, the form is submitted and sent to process.php</input>
</form>
0

Source: https://habr.com/ru/post/1714373/


All Articles