Javascript to triger submit button

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); " />
TrialUserAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
If what you really want to do is submit the form, why not call "submit()" on the form's id?
TrialUserAuthor 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
Tom BeckCommented:
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.
Determine the Perfect Price for Your IT Services

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

TrialUserAuthor 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
Tom BeckCommented:
Just noticed you have "keycode". It's "keyCode" (camel case).
TrialUserAuthor 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;            
            }
Tom BeckCommented:
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".
TrialUserAuthor 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">
Tom BeckCommented:
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;            
            }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TrialUserAuthor Commented:
ok, thanks that worked
Tom BeckCommented:
You're welcome. Thanks for the points.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.