TrialUser
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("t xtUserName ").focus() ;
};
function jftnSubmit(e) {
if (e.keycode == 13) {
alert('d enter f');
var ButtonID = btnLogin.id;
document.getElementById(bt nSearchID) .click();
}
return false;
}
</script>
<input id="txtUserName" autofocus runat="server" type="text"
class="form-control" placeholder="Username" maxlength="50" onclick="jftnSubmit(event) ; " />
<script lang="javascript" type="text/javascript">
window.onload = function () {
document.getElementById("t
};
function jftnSubmit(e) {
if (e.keycode == 13) {
alert('d enter f');
var ButtonID = btnLogin.id;
document.getElementById(bt
}
return false;
}
</script>
<input id="txtUserName" autofocus runat="server" type="text"
class="form-control" placeholder="Username" maxlength="50" onclick="jftnSubmit(event)
If what you really want to do is submit the form, why not call "submit()" on the form's id?
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
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("f ormId").su bmit();
Whatever your form id is.
<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("f
Whatever your form id is.
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
Just noticed you have "keycode". It's "keyCode" (camel case).
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("b tnLogin"). click();
}
return false;
}
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("b
}
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".
ASKER
The button is an asp.net content page
When i view source this is what I see:
<button onclick="if (typeof(Page_ClientValidat e) == 'function') Page_ClientValidate(''); __doPostBack('ctl00$cphBod y$btnLogin ','')" id="cphBody_btnLogin" class="btn-standard" type="submit">
When i view source this is what I see:
<button onclick="if (typeof(Page_ClientValidat
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok, thanks that worked
You're welcome. Thanks for the points.