Link to home
Start Free TrialLog in
Avatar of TrialUser
TrialUserFlag 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); " />
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

If what you really want to do is submit the form, why not call "submit()" on the form's id?
Avatar of 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
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.
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
Just noticed you have "keycode". It's "keyCode" (camel case).
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;            
            }
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".
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
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, thanks that worked
You're welcome. Thanks for the points.