INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Blur events going to two textboxes

Blur events going to two textboxes

(OP)
I have an event that uses a class selector for 10 textboxes.

I am checking to see if the value is numeric. If there is an error I am doing a this.focus() to go back to the textbox I just left. The problem is that end up getting 2 blur events.

For example, if I am on the 3rd textbox, enter a value then tab to the 4th textbox. This causes a blur on the 3rd textbox as well as the 4th checkbox when the focus goes back to the 3rd textbox.

How do I solve this problem?

CODE

$(".numeric0").blur(function (event) {
      if (!isNumericOrBlank($(this).val(), 0))
      {
         $(this).parent().find('.validMessage').show();
         this.focus();
      }
   }); 

Thanks,

Tom

RE: Blur events going to two textboxes

Unfortunately that's the way blurr works.

When the element loses focus for any reason blurr activates. Since you are re-focusing the previous element, the current one loses focus, and activates its own blurr.

Best bet is to not re-focus the previous element but simply point to it, or highlight it in some way so the user knows there was an error. Let the user click back in when they are ready to fix it.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Blur events going to two textboxes

(OP)
That's fine, but not how we want the behavior to work.

I did figure out a way to make it work by setting a global variable to tell it to not process anything if bluring is turned off.

Works perfect. This is similar to an issue I had with windows forms where you cannot prevent an event from firing but in some instance it will fire. So you set and unset a variable to allow the event to fire but you just don't do anything in the event code.

CODE

var focusOK = true;

$(".numeric0").blur(function (event) {
      if(focusOK)
      {
          if (!isNumericOrBlank($(this).val(), 0))
          {
             $(this).parent().find('.validMessage').show();

             focusOK = false;        // prevent blur code to work
             this.focus();
          }
       }
       else
       {
          focusOK = true;     // reset focusOK to allow blur code to work
       }
   }); 

This blur event is on all the textboxes in the group.

Thanks,

Tom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close