Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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.
Reply With Quote
  #2 (permalink)  
Old October 2nd, 2009, 12:18 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #3 (permalink)  
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>
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:59 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.