Solved

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

Posted on 2004-04-30
4
2,107 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
  • 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 125 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now