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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable function while inside form element 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
I have a function that focuses the user to a form on the page (a search box). I am now needing to implement another form element on the same page. And i just realized my function isn't going to work very well on that page.

My function sends the user to the search box when the press the letter S on the keyboard. Is it possible to throw in if statement in there that first checks to see if they are already in a form element on the page. And if they are, then not do anything? Here's my current function:
Code:
function sendToSearch(e) {
  e = (e) ? e : event;
  var keycode = (e.keyCode) ? e.keyCode : (e.which) ? e.which : false;
  character=String.fromCharCode(keycode);
  if(keycode == "83" || keycode == 83 || character == "s"){
    document.forms['search_box'].elements['search'].focus();
  }
}
document.onkeypress = sendToSearch;

_______________
_brian.
 
Instead just deactivate the onkeypress function after the search box gains focus, and then reactive the function when the box loses focus. Try something like this:
Code:
<script type="text/javascript">
function removeShortcut() {
   document.onkeypress = function () {return true}
}

function addShortcut() {
   document.onkeypress = sendToSearch;
}
</script>
<input type="text" name="search" onfocus="removeShortcut()" onblur="addShortcut()" />

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Excellent. I forgot all about that. I remember now I had to do something like that for a past application. Thanks a lot!

_______________
_brian.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top