• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't move focus to the control because it is invisible

 
russell stoneyroyd
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When trying to get focus on an element in IE I get the following javascript error message:

'Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus'

I can resolve it by the code above. But not sure what I'm checking. I realise that first checking visibility of the input type ie whether it's hidden. Whether the style of the display is set to not display and whether that element has disabled. I'm not 100% certain about the second check the display. This fix works but would like to understand better what the 2nd check means.
Any help would be greatly appreciated.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second one is to check if CSS's style.display property has been set to "none", which would mean that the component exists but is not displayed.
I'm not sure if it's the correct way to check if you can set the focus or not. Do you intend to do it for all your focusable elements ?
 
russell stoneyroyd
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the response.
Yeah basically there is a bunch of html input types rendered on a page the code loops through to look for the first focusable input type and then gives focus to it. So this is the check to see what is focusable. Have tried element.focus != null but this didn't work.
Do I make sense?
 
Andrey V. Ovcharenko
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can check up in addition property "visibility". However, the parent of an element can be invisible. Therefore, the call of a method "focus" is better for concluding in the block "try".
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrey,

3 years later your solution has helped me
 
Sun Boxit
Greenhorn
Posts: 1
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrey V. Ovcharenko wrote:You can check up in addition property "visibility". However, the parent of an element can be invisible. Therefore, the call of a method "focus" is better for concluding in the block "try".


Thanks Andrey, try/catch the method of focus is the simplest way to fix the IE bug!
DO NOT use so many if judgement, who works well will work well, let it to be!

@see
http://bugs.jquery.com/ticket/10859
http://bugs.jquery.com/ticket/11054

WHY jQuery DO NOT fix the "bug"?
In 1.6.x we trapped and discarded *any* errors thrown by an inline event handler.
That is not good because it masks problems that the handler may have.
We are trying to remove any of these omnibus try/catch situations in the jQuery code base to prevent that.
So this error was occurring before, but just masked.

I think IE8 is justified in being upset that you're trying to set focus to an element the user cannot see or interact with.
Most other browsers seem to ignore it since it's not a valid request, but there's no W3C guideline that I know of for what to do when someone asks to focus an invisible element.
An error is probably better than silence when it comes to debugging.

If this is being caused by a plugin then the plugin should be fixed.
Solutions include showing the element before setting focus to it, or using .triggerHandler() if you just want to run handlers and not actually set focus.

by dmethvin

This resolution seems a bit strange to me, but perhaps I don't quite understand jQuery goal.
From where I stand - jQuery is suppose to provide consistent behaviour across browsers and this is not it.

by anonymous

jQuery provides consistent behavior for a valid set of inputs and actions.
Trying to focus an invisible element isn't in that set.
Making things consistent across browsers shouldn't mean making all browsers hide an mistake if any of them hide the mistake.

by dmethvin
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, as pointed out, there is no bug to fix.

"ufologist dream", please check your private messages for an important administrative matter. Thanks.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic