views:

1159

answers:

5

Hi All,

Good Morning...

I am using java script in each page to trigger the Enter key press Event inside the textbox. It is working fine. Now i want to place the code in the .js file for 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 dont want to hard code the control id. Can anybody help me please..

A: 

If you're using jQuery you can do it like this:

$("input[id*=ibtnSubmit]").click()
sirrocco
A: 

If you want the form to submit when the user presses the enterkey, then you don't need javascript. It will do so automatically. Just put the textbox inside a form element with an action:

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

You can use your id parameter instead of the ControlID, and when you use this function in your pages, you can pass the 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-behind you can assign the keypress event:

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

But wait, that functionality is already done on ASP .NET, you can use ASP:Panel controls to wrap your common controls, and you can assign a DefaultButton for the panel to indicate which button gets clicked when the Panel control has focus and the user presses the Enter key.

CMS
A: 

i am getting the following error.

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(id).click();
       } 
   }

Error : Microsoft JScript runtime error: 'window.document.getElementById(...)' is null or not an object

Check what the id parameter contains. You can see the source of the Page on your browser to know exactly what the client-side is executing on the event assignment.
CMS
onkeyup="javascript:EnterKeyPress('ibtnSubmit.ClientID',event);"in contains 'ibtnSubmit.ClientID'
You should assign the onkeyup event on the code-behind as in my answer, `'ibtnSubmit.ClientID'` means nothing to the client-side.
CMS
instead of using code-behind is there any other way to resolve this problem?
A: 

Code:

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(id).click();
       } 
   }

onkeyup="javascript:EnterKeyPress('<%=ibtnSubmit.ClientID%>',event);"

<%=ibtnSubmit.ClientID%>' - not working