Avatar of TrialUser
TrialUser
Flag for Afghanistan asked on

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); " />
ASP.NETJavaScriptHTML

Avatar of undefined
Last Comment
Tom Beck

8/22/2022 - Mon
Tom Beck

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

ASKER
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 Beck

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
TrialUser

ASKER
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 Beck

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

ASKER
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;            
            }
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tom Beck

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".
TrialUser

ASKER
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">
ASKER CERTIFIED SOLUTION
Tom Beck

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TrialUser

ASKER
ok, thanks that worked
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Tom Beck

You're welcome. Thanks for the points.