Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Page reloading when hitting enter in textbox, not executing code
Old 11-26-2008, 03:04 AM Page reloading when hitting enter in textbox, not executing code
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
I have a script that allows the user to enter a number into a text box and then click the button (type="button" not submit) and then it just updates a <div> with the calculated value. That all works fine. My problem is that if I just entered the number into the textbox and then I hit the enter key while my cursor is still inside the text box it refreshes the page instead of executing the javascript that it is supposed to. Originally I thought I had just made a stupid mistake and made it type="submit" instead of "button" but thats not it.

Any ideas? Thanks
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-26-2008, 05:59 AM Re: Page reloading when hitting enter in textbox, not executing code
Experienced Talker

Posts: 32
Name: Dan
Location: New Zealand
Trades: 0
Can we see the code for the form?
__________________

Please login or register to view this content. Registration is FREE
/ PHP Web Development and Design Services

Please login or register to view this content. Registration is FREE
Awesome new Facebook app
Rumadon is offline
Reply With Quote
View Public Profile Visit Rumadon's homepage!
 
Old 11-26-2008, 06:01 AM Re: Page reloading when hitting enter in textbox, not executing code
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,528
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
disable the enter key.

Code:
<script type="text/javascript">

function killEnter(keyEvent) {
  var keyEvent = (keyEvent) ? keyEvent : ((event) ? event : null);
  var node = (keyEvent.target) ? keyEvent.target : ((keyEvent.srcElement) ? keyEvent.srcElement : null);
  if ((keyEvent.keyCode == 13) && (node.type=="text"))  {return false;}
}

document.onkeypress = killEnter;

</script>
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-26-2008, 06:46 AM Re: Page reloading when hitting enter in textbox, not executing code
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
It may be overkill, but remember that in case you already have an onkeypress event, you must go through the DOM functions to stack several event handlers on one event.

https://developer.mozilla.org/en/DOM...dEventListener
Code:
try{
  document.addEventListener('keypress',killEnter, true);
}
catch(hello_my_name_is_IE){
  document.attacheEvent('onkeypress',killEnter);
}
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-28-2008, 12:04 AM Re: Page reloading when hitting enter in textbox, not executing code
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Isnt there any ways that instead of killing the enter button I can just make it click the button. I would still like to give users the optipn of pressing the enter button if possible.

Thanks
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 11-28-2008, 04:02 AM Re: Page reloading when hitting enter in textbox, not executing code
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
In that case, rather than applying the killEnter function on the "document" element, assign it to the form input or the form itself (you need to test it).

It will have the effect of making that killEnter function called only when the focus is on specific elements, not anywhere on the page.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Page reloading when hitting enter in textbox, not executing code
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23231 seconds with 12 queries