1

I want blur select menu when it is open. I have made a small function when you hover the mouse on a tag then blur function will call. It is working fine in other browser but not working in Chrome. fiddle

Reproduce bug : click and open select menu and hover anchor tag, open select menu should be closed

<head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
   $('a').hover(function(){
    $('select').blur();
   })
})
</script>
</head>
<body>
<select>
<option>Test</option>
<option>Test</option>
<option>Test</option>
<option>Test</option>
<option>Test</option>
<option>Test</option>
<option>Test</option>
</select>
<a href="#">hover me</a>
</body>
</html>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Jitender
  • 7,593
  • 30
  • 104
  • 210

2 Answers2

3

I had the same problem, and the idea was clear: you cannot blur an opened select element, because Chrome and Safari waits for the answer. BUT there is a workaround that you may find acceptable.

First, you cannot set focus to another element while the "select" is still active, so I have removed it by applying a "display:none". After that I trigger a click event on another harmless element. And then I re-add the select back, by setting a display:block. In this case, the code would look like(i will add a span to click on):

.......
<select>
<option>Test</option>
......
</select>
<a href="#">hover me</a>
<span class="clickable">element clicked</span>
.......
<script type="text/javascript">
$(document).ready(function (){
    jQuery("a").mouseenter(function (){
        $("select").css({'display':'none'});
        $("span.clickable").focus().click();
        $("select").css({'display':'block'});
    });    
});
</script>
ATud
  • 74
  • 4
  • Doesn't it affect accessibility to drop the focus into a random element? It seems like you're stranding users who tab through elements. Is there an accessibility-friendly solution available? – AJFarkas Dec 12 '14 at 18:30
  • This did the trick. `$("select").blur()` worked in IE, Edge, and Chrome - but not Firefox. `$("select").css({ "display": "none" }).blur().css({ "display": "inline" });` did the trick across all of them. – Kris Krause Apr 07 '17 at 21:00
1

You need to use .trigger() for that

<select id="selectboxId">
.......
</select>

   $ ('#selectboxId').trigger('blur');
PSR
  • 39,804
  • 41
  • 111
  • 151