Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Focus on a non input element

Tags:

html

focus

scroll

I have a script that shows a popup, and fairly often, the content is taller than the window and requires scrolling. The popup box is within a container div that has an overflow so the popup can be scrolled around without interfering with the scroll position on the rest of the page.

The issue is that when the popup box is displayed, when the up and down arrows are pressed, nothing happens as the popup box in the holder is not focused. Only when I click on an element within the container, then the arrows start working. Is it possible to perform a similar function to the 'click on an element' method described before, except in javascript. I tried .focus and other similar commands, but none worked.

like image 796
topherg Avatar asked Oct 05 '12 16:10

topherg


People also ask

Can we apply focus on div?

Yes - this is possible. In order to do it, you need to assign a tabindex... A tabindex of 0 will put the tag "in the natural tab order of the page". A higher number will give it a specific order of priority, where 1 will be the first, 2 second and so on.

Can Li be focused?

You cannot normally focus li elements. But you can hover them, so you should use li:hover instead of li li:focus .

How do I remove input from focus form?

Use the blur() method to remove the focus from an element, e.g. input. blur() . If you need to remove the focus from the currently active element without selecting it, call the blur method on the activeElement property - document.


1 Answers

Aha, sorted it. Set the holder's tabIndex to -1 and then .focus() worked on selecting it

like image 152
topherg Avatar answered Oct 05 '22 07:10

topherg