Javascript to triger submit button

TrialUser
TrialUser used Ask the Experts™
on
When user clikcs enter insid a textbox, I want to triger the submit button click, Ihave the following code but code inside if is not executed. Please help.

  <script lang="javascript" type="text/javascript">
        window.onload = function () {
            document.getElementById("txtUserName").focus();
        };
        function jftnSubmit(e) {
             if (e.keycode == 13) {
                alert('d enter f');
                var ButtonID = btnLogin.id;
                document.getElementById(btnSearchID).click();
               
             }
             return false;
           
            }      
   
    </script>

 <input id="txtUserName" autofocus  runat="server" type="text"
                        class="form-control" placeholder="Username" maxlength="50" onclick="jftnSubmit(event); " />
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
If what you really want to do is submit the form, why not call "submit()" on the form's id?

Author

Commented:
Sorry, i am not following your comment. When the user clicks enter inside the textbox, I want to click on the "submit" button.
Also, this is a content page, so how would I set this up? Thanks
Top Expert 2015

Commented:
First you want to capture any onkeyup event when the textbook has focus, not the click event.

<input id="txtUserName" autofocus  runat="server" type="text"
                        class="form-control" placeholder="Username" maxlength="50" onkeyup="jftnSubmit(event); " />

To my other comment, why simulate the submit button click which in turn submits the form? Just submit the form.

document.getElementById("formId").submit();

Whatever your form id is.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
The code inside my if statement doesnt seem to be executed at all. Even when I click enter, the alert inside If statement is not shown
Top Expert 2015

Commented:
Just noticed you have "keycode". It's "keyCode" (camel case).

Author

Commented:
Tom, I corrected the keyCode. Thanks
The alert inside if works, but does not trigger the button click. Please help.
function jftnSubmit(e) {
           
            if (e.keyCode == 13) {
                alert('inside if');
                document.getElementById("btnLogin").click();                              
             }
             return false;            
            }
Top Expert 2015

Commented:
No idea why that would not work. I've created a simple test here on jsfiddle and it works. Perhaps your form is submitting when you hit the Enter key and so canceling the button click. Or maybe because this is an asp.net page, the id of the button on the rendered page is not actually "btnLogin".

Author

Commented:
The button is an asp.net content page
When i view source this is what I see:
 
                    <button onclick="if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(''); __doPostBack('ctl00$cphBody$btnLogin','')" id="cphBody_btnLogin" class="btn-standard" type="submit">
Top Expert 2015
Commented:
Well, there ya' go. The id is "cphBody_btnLogin".

Try this:

if (e.keyCode == 13) {
                alert('inside if');
                document.getElementById('<%= btnLogin.ClientID %>').click();                              
             }
             return false;            
            }

Author

Commented:
ok, thanks that worked
Top Expert 2015

Commented:
You're welcome. Thanks for the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial