aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes can't move focus to the control because it is invisible Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "can Watch "can New topic
Author

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

russell stoneyroyd
Greenhorn

Joined: Sep 12, 2006
Posts: 9
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
russell stoneyroyd
Greenhorn

Joined: Sep 12, 2006
Posts: 9
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

Joined: Feb 15, 2007
Posts: 7
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

Joined: Jul 29, 2008
Posts: 275

Thanks Andrey,

3 years later your solution has helped me


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Sun Boxit
Greenhorn

Joined: Sep 20, 2012
Posts: 1

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

Joined: Jan 10, 2002
Posts: 60057
    
  65

Because, as pointed out, there is no bug to fix.

"ufologist dream", please check your private messages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't move focus to the control because it is invisible
 
Similar Threads
how to void focus on disabled fields
error msg when I try to pull up a web page for my online schooling
Problem in IE 5
IE, javascript, moving to anchors, and setting focus
TabLayoutPanel.selectedTab error