/    Sign up×
Community /Pin to ProfileBookmark

blur/focus on SELECT in IE

THE PROBLEM:

On IE, when a user clicks into a SELECT control and clicks on any option in the list, then moves the mouse out of the SELECT control, focus remains in the SELECT control until the user clicks elsewhere on the page. Firefox handles this much more gracefully. Problem is, our users are clicking an option, then using their mouse wheel to scroll to down the page. What is actually happening is they are scrolling through the options in the SELECT and, inadvertantly, selecting another OPTION in the list. They often don’t notice this and submit forms with bogus options.

MY SOLUTION, SO FAR:

I have written the following code, in hopes of including it at the end of each of my HTML/JSP files to work around this problem:

<script language=”javascript”>
function blurSelect()
{
this.blur();
document.getElementById(“bcmInd”).focus();
return true;
}

/* loop through all form fields looking for SELECTs, when I find one, if it does
*
not already have an onchange handler, I set it’s onchange handler to
* blurSelect()
*
/
var selectCount = 0;
for (formCnt=0; formCnt<document.forms.length; formCnt++)
{
for (elementCnt=0; elementCnt<document.forms[formCnt].elements.length; elementCnt++)
{
if (document.forms[formCnt].elements[elementCnt].tagName == “SELECT” && document.forms[formCnt].elements[elementCnt].onchange==null)
{
document.forms[formCnt].elements[elementCnt].onchange=blurSelect;
}
}
}
</script>

MY QUESTIONS:

  • 1.

    I am using the onchange event on the SELECT control to trigger the call to blur. It does not fire if the user clicks into the SELECT and does not change the selected value, however. I tried onclick (blurs when dropping down the list), onmouseout (blurs when moving into the selectable options) and onmousewheel (never seems to fire at all). Any suggestions on a better event to use?

  • 2.

    Just calling blur() on the SELECT control does not actually work. It seems I must call focus() on an actual visible form element to remove focus from the SELECT. This is a problem because I’m trying to make this generic and I don’t have a form element on every screen with the same name. It seems like this should be possible because when you click on whitespace on the form, it removes focus from the SELECT, without placing it on a visible form element. It’s also a problem because some of my forms are long and sending focus to a form field willy-nilly can scroll the page up or down . . . very annoying.

  • Any help is much appreciated.

    Joe

    to post a comment
    JavaScript

    6 Comments(s)

    Copy linkTweet thisAlerts:
    @balloonbuffoonJan 05.2006 — Well you can see about using
    [CODE]document.formname.elements[#].focus()[/CODE]
    And just have the number be the SELECT's element number +1. But you would have to know the element number of that particular select box. That only answers your second question; I don't really think there is a better event to use than onchange() so I can't really answer your first question.

    Good Luck

    --Steve
    Copy linkTweet thisAlerts:
    @CrazyMerlinJan 05.2006 — you could probably just create a div with a single period as it's contents.

    give it a name and send the focus to that after a selection has been made.
    Copy linkTweet thisAlerts:
    @KorJan 06.2006 — 
    What is actually happening is they are scrolling through the options in the SELECT and, inadvertantly, selecting another OPTION in the list. They often don't notice this and submit forms with bogus options.
    [/quote]


    You may use onmousewheel event and return it false.

    <select onmousewheel="return false">

    or, if you want it dynamically
    [code=php]
    <script type="text/javascript">
    onload = function(){
    var sel = document.getElementsByTagName('select');
    for(var i=0;i<sel.length;i++){
    sel[i].onmousewheel=function(){return false}
    }
    }
    </script>
    [/code]
    Copy linkTweet thisAlerts:
    @joeschweickartauthorJan 10.2006 — Thanks to all for your replies. After finding out that onmousewheel is newly available in IE 6 (we were on 5.5), I found a way to upgrade our clients to IE 6 (MS Updates are blocked by corporate IT). The solution by KOR worked beautifully, essentially disabling the mouse wheel inside the select controls.

    The problem creating the dummy div to shift focus to, was that, dependent on where the div appeared compared to the current select control, refocusing may cause confusing vertical screen scrolling on long forms.

    The problem of jumping to the next form element is that our users usually upadate a couple of random fields on the screen at a time. Jumping to the next form field, which must have a tabIndex property to accept focus, might put focus on another select, which would create the same problem, just on a different control. This did spark a curiosity question, however: If you are going through a list of select controls by using getElementsByTagName('SELECT'), and you have a particular SELECT control, how do you find its next sibling element in the document.forms.elements collection?

    Thanks again for your assistance.
    Copy linkTweet thisAlerts:
    @KorJan 10.2006 — 
    how do you find its next sibling element in the document.forms.elements collection?
    [/quote]

    Hm...

    In classical javascript way you might use a simple loop

    function findNextSib(el){

    var myEl = document.forms[[I]name_or_index[/I]].elements;

    for(var i=0;myEl.length;i++){

    if(el==myEl[i]&&i<(myEl.length-1))){

    var nextSibling=myEl[i+1]

    }

    }



    Using DOM2 Core might be easier, but u should take care that IE and Moz count in different ways the nodeChilds...



    What is in your mind?
    Copy linkTweet thisAlerts:
    @ashton75Jun 16.2006 — 
    ...It seems I must call focus() on an actual visible form element to remove focus from the SELECT. ... It's also a problem because some of my forms are long and sending focus to a form field willy-nilly can scroll the page up or down . . .[/QUOTE]


    Probably too late for you, but for anyone else facing this problem, something like this works:

    [CODE]<select onChange='body.focus()' >
    <option Value="1">1</option>
    <option Value="2">2</option>
    <option Value="3">3</option>
    </select>[/CODE]


    body.focus() won't scroll the page. You don't need to do an explicit blur(), since you're moving focus.
    ×

    Success!

    Help @joeschweickart spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...