Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 1st, 2009, 07:00 PM
Registered User
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default onBlur event - determining what caused the blur?

Hi all - I have several text boxes on my page, and I've set the onBlur handler for each to call a function. The problem is, I only want my event handler to take action if the blur was caused by the user clicking the mouse somewhere else on the page. I do not want it to do anything if the blur happened because the user tabbed away from the element, or if the focus was programatically set to a different element.

Is there any way to determine from the event object if the tab key was pressed, or if the mouse button was pressed? I've tried looking at event.button and event.keyCode but they both always seem to be set to 0 for an onBlur event. Is there any other way to tell?

Part of the issue for me is that the onBlur event seems to be the first event that fires (before, for example, onKeyPress or onMouseDown).

My target browser for the moment is IE7.

Thanks in advance for any help.
 
Old October 2nd, 2009, 12:18 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The blur event is separate from mouse click or key press events, so that's why you won't see a useful value for keyCode or button on the event object in an onBlur handler.

Based on your criteria, I'm not sure how you're going to filter out the conditions you don't want. You might be able to capture the keyDown event to detect the tab key (save to some global var), then in the blur event handler you can test for the last key being the tab. But if you want to filter out programmatic focus changes, you'll probably need to program that in to all the places that might move focus away from that control in order to set a flag that it was a programmatic move versus a user action.
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
 
Old October 2nd, 2009, 12:29 PM
Registered User
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

Quote:
Originally Posted by planoie View Post
The blur event is separate from mouse click or key press events, so that's why you won't see a useful value for keyCode or button on the event object in an onBlur handler.

Based on your criteria, I'm not sure how you're going to filter out the conditions you don't want. You might be able to capture the keyDown event to detect the tab key (save to some global var), then in the blur event handler you can test for the last key being the tab. But if you want to filter out programmatic focus changes, you'll probably need to program that in to all the places that might move focus away from that control in order to set a flag that it was a programmatic move versus a user action.
So basically my four text boxes would each call the same function onBlur, but each would pass a different parameter.

Like this:

Code:
<input type="text" onBlur="blurHandler(1)"></input>
<input type="text" onBlur="blurHandler(2)"></input>
<input type="text" onBlur="blurHandler(3)"></input>
<input type="text" onBlur="blurHandler(4)"></input>
So what I decided to do is to make a proxy function for onBlur, and just have it save the parameter value that was passed. Then the mouseeventhandler checks to see if that saved value is null, and if its not, run the original blur event handler, passing it that param. Then it sets the saved value back to null.

Code:
<script>

var buttonNumberProxyValue;

function blurHandlerProxy(buttonNumber)
{
    buttonNumberProxyValue = buttonNumber;
}

function blurHandler(buttonNumber)
{
      //do something
}

function mousedownhandler()
{
      if (buttonNumberProxyValue != null)
      {
              blurHandler(buttonNumberProxyValue);
      }
   
      buttonNumberProxyValue = null;
}
</script>

<body onmousedown="mousedownhandler()">

<input type="text" onBlur="blurHandler(1)"></input>
<input type="text" onBlur="blurHandler(2)"></input>
<input type="text" onBlur="blurHandler(3)"></input>
<input type="text" onBlur="blurHandler(4)"></input>

</body>





Similar Threads
Thread Thread Starter Forum Replies Last Post
TEXT BOX BLUR pallone Javascript How-To 4 September 25th, 2008 10:59 AM
calling a codebehind function on onblur event vishnupriya.kamalakaram ASP.NET 1.0 and 1.1 Basics 2 July 7th, 2006 08:25 AM
How to call a codebehin function on onblur event?? kv Priya ASP.NET 1.0 and 1.1 Professional 1 June 14th, 2006 02:43 PM
problem caused by data transfer chacquard Access VBA 1 January 8th, 2005 10:59 AM
Alternative to onBlur() event ashu_gupta75 Classic ASP Basics 3 April 30th, 2004 09:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.