[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

onblur and onkeypress:  onblur triggered on each keystroke when both events are included

Posted on 2004-04-30
4
Medium Priority
?
2,442 Views
Last Modified: 2008-03-04
I am trying to build an input validation function that will be called either when the user leaves the text field or when the user hits <enter> inside the tex field.  In order to do that, I need to have the function (or two functions) called when 2 types of events are triggered:  onblur (for when the text field loses focus) and onkeypress (where I can check if the keycode == 13, and, if it does, to try to validate; if the keycode != 13, I ignore it).

What I am discovering is that when I add the onblur event to the text field, it is only triggered when the text field loses focuses.  Similarly, if the keycode is the only event, it gets called on every keystroke.  However, if I have both events inside the <input> tag, then the onblur event is triggered BOTH on every keystroke and when the text field loses focus.  This means when the user types a keystroke, first the onblur event is triggered then the keystroke event is triggered.  (I am using IE6).

Is there any way to include both events but to have them triggered for the appropriate reasons and not to have the onblur event triggered from keystrokes?
0
Comment
Question by:msalamon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 10960955
I cannot verify your observation in my IE6.0
Do you perhaps use alert() in your onkeypress handler? Then of course you have every time a blur.

Check this:

<html>
<body>
<form>
<input type=text name="Input" onBlur="window.status+='blur;'" onKeyPress="window.status+='press;'">
</form>
</body>
</html>

0
 

Author Comment

by:msalamon
ID: 10961294
Needless to say, you're a genius!  Thanks.  However...

In the situations where the text entered is not validated, i use aler() to inform the user of the type of error.  Thus, in situations where the user hits <enter> and the text is not validated, an alert is issued.  This triggers onblur, which calls the function, and then the function is called again, so the alert is shown twice. Is there any way to "smother" the triggering of onblur when alert() is called, or to detect that onblur has been called by an alert so I can ignore it, or something similar to that?
0
 

Author Comment

by:msalamon
ID: 10961325
One more thing.  When the user hits <enter>, whether it is validated or not, the text inside the text field disappears.  Any idea how to stop that?
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 10961494
The text does NOT disapear normaly, so its again some side effect.

Ok, your question was how to suppress the onBlur event as long as your onKeyPress is prcessing its business.
Simply like this:

<html>
<body>
<form>
<input type=text name="Input" onFocus="cB=true" onBlur="if(cB)alert('blur')" onKeyPress="cB=false;kC=event.keyCode;alert(kC);cB=true;if(kC==13){this.blur();return false;}">
</form>
</body>
</html>

0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question