...

View Full Version : Textbox Enter Key Issue



tvb2727
07-30-2011, 08:38 AM
I have a textbox on my webpage where you can enter a title. I then have a button that goes to my javascript and starts a timer. Well if the user presses enter on the text box I want it to start the time. Well it goes to the code, but then like refreshes the website and starts from the start. How can I get around this? When I click the button there is no issues at all.

Textbox:

<input type="text" id="titleMainT"
value="N/A" size="27" onkeypress="startTimer();"/>

Button:

<input type="button" id="startbutton" value="Start Timer" onclick="startTimer();" />

Philip M
07-30-2011, 08:47 AM
onkeypress will trigger the script each time a key is pressed.
Try onblur instead.

If your problem is that pressing the enter key causes a form to submit, the the solution is to disable the enter key:-
onkeypress = " return !(window.event && window.event.keyCode == 13)";


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

tvb2727
07-30-2011, 09:10 AM
onblur resets it as well.

tvb2727
07-30-2011, 09:15 AM
it also puts this at the end of my web address...

?selectfamily=Arial%2C+Helvetica%2C+sans-serif&selectFont=30&selectcolorBACKGROUND=WHITE&selectcolorFONT=BLACK

tvb2727
07-30-2011, 09:42 AM
Yea, my issue is the form submits. I just want them to process the website by the button. Is there a chance I could fix that and make sure when they press enter on the textbox it does nothing?

Philip M
07-30-2011, 09:48 AM
Yea, my issue is the form submits. I just want them to process the website by the button. Is there a chance I could fix that and make sure when they press enter on the textbox it does nothing?

Yes, disable the enter key as I indicated in Post #2.

A more extended script which may be useful if there are multiple input fields is


<script type = "text/javascript">

function onKeyPress (e) {
var keycode;
if (window.event) {keycode = window.event.keyCode}
else if (e) keycode = e.which;
else return true;
if (keycode == 13) {
alert ("Please use the tab key (not the enter key) or click\nwith your mouse to move between the form fields.");
return false;
}
return true;
}

document.onkeypress = onKeyPress;

</script>

tvb2727
07-30-2011, 09:54 AM
You just fixed my issue. Thanks a lot! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum