Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling Submit/Enter Keyboard Button in an Input Form Box

Status
Not open for further replies.

kennygadams

Programmer
Jan 2, 2005
94
US
Hi,

How do you disabling submit/enter keyboard button for a specific text input form box.

Kenny
 
You want to disable the submit button when pressing enter from the keyboard??

Can you give your specific example?




[monkey][snake] <.
 
Thanks for the quick reply!

Here's the example page
I want the user to click the "Clipart Search" button to go to the search results page. How do i disable the enter/submit keyboard button for that specific input box?

Thanks,
Kenny
 
In the example you gave, focus is given to the submit button once the text cursor is in the textbox.

I'm not really sure why you'd want to do this, but here's an example.

On the submit button of the page, put an onkeypress event handler and put an onsubmit event handler on your <form> tag:

Code:
<input type="submit" value="Press Me" onkeypress="checkPress(event)" />
AND 
<form method="post" onsubmit="return checkEnter(event)">

You javascript will check to see if the "Enter" button was pressed when the submit button has focus. If it was, it returns false on the onsubmit, so the form won't be submitted.

Code:
<script type="text/javascript">
var enterWasPressed = false;

function checkPress(e) {
   if (e.keyCode == 13) {
      enterWasPressed = true; 
   alert("hee");
   }
}

function checkEnter(e) {
   if (enterWasPressed) {
      return false;
   }
}
</script>





[monkey][snake] <.
 
Thanks for getting back to me. At the moment I don't have time to test this. This is going to be used in an Ajax input box and when the user clicks on the "Clipart Search" button it will give them the results on the current page. As the form is right now if the user clicks the submit/enter button on their keyboard then it takes them to a differant page but if they click on the button they will stay on the current page and the image results will display as they should.

Code:
<form name='search'>
<input type='text' size='30' name='search_term' value="" >
<input type='button' value='Clipart Search' onClick='search_call()'>
</form>

Thanks,
Kennhy
 
In that case, put the onkeypress event handler on the <body> tag, NOT here:
Code:
<input type="submit" value="Press Me" onkeypress="checkPress(event)" />

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top