Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why $(this).attr('id'); function not returning value ? 9 53
Index on a Table 6 25
xjs: how to get sum of value in for loop 2 19
Html Newline 7 21
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

829 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