24688

jQuery .has(“:focus”) in FF/Chrome/Safari

I have a textbox that has filter as you type results below it. I have jQuery that detects when focus leaves the textbox so it can hide the results div. BUT I don't want to hide the results if the results are click on so that I can use that click to redirect to a different page for that item.

I'm using .has(":focus") to check if my results div <strong>or any of its children</strong> have focus. My problem is that this only works in IE as far as I can tell. Anyone know why?

$("#TextBox").blur(function () { if ($("#ResultsDIV").has(":focus").length == 0) { //if you click on anything other than the results $("#ResultsDIV").hide(); //hide the results } });

<strong>UPDATE:</strong> Thanks to Climbage I've got this working...

var resultsSelected = false; $("#ResultsDIV").hover( function () { resultsSelected = true; }, function () { resultsSelected = false; } ); $("#TextBox").blur(function () { if (!resultsSelected) { //if you click on anything other than the results $("#ResultsDIV").hide(); //hide the results } });

<strong>Working Example:</strong> http://jsfiddle.net/cB2CN/

Answer1:

The problem is that all not elements on the DOM actually accept focus, and it's up to the browser to decide which ones do. See Which HTML elements can receive focus?

Maybe have a hover event on your #ResultsDIV that sets a global variable to determine if the element has focus when you click on it.

Recommend

  • Why is LinkButton not executing the Click function from code behind
  • How to call php into wordpress page ajax [closed]
  • Add and remove checkbox events dynamically depending on some business logic?
  • Reusable jquery ajax requests
  • PHP Handling Namespace with SimpleXML
  • Angular2 emit event up to the DOM tree
  • Firebase, only get new children
  • How to save dynamically created textboxes and their values
  • How to add a focus style to an editable ComboBox in WPF
  • How to disable all widgets inside Panel or inside Composite?
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Recording logins for password protected directories
  • Email format validation in mvc3 view
  • Splitting given String into two variables - php
  • Can Jackson SerializationFeature be overridden per field or class?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Check if a string to interpolate provides expected placeholders
  • javascript inside java/jsp code
  • ILMerge & Keep Assembly Name
  • Join two tables and save into third-sql
  • htaccess rewriting URLs with multiple forward slashes
  • How to model a transition system with SPIN
  • Display Images one by one with next and previous functionality
  • Large data - storage and query
  • ORA-29908: missing primary invocation for ancillary operator
  • Web-crawler for facebook in python
  • WOWZA + RTMP + HTML5 Playback?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • A cron job substitute?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Understanding cpu registers
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Add sale price programmatically to product variations
  • Converting MP3 duration time
  • How to push additional view controllers onto NavigationController but keep the TabBar?