Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery focus does not trigger css focus

Tags:

jquery

css

There seems to be a problem when setting focus to an element using jquery. It apparently does not trigger the :focus css property set on an element.

For example in my css I have:

div.item1:focus { border:2px solid red; }

in my jquery I have:

$("div.item1").focus();

The focus is set but there is no red border applied to the element.

like image 273
aris Avatar asked Jun 28 '12 12:06

aris


2 Answers

div elements do not use the :focus selector .. see the CSS2 spec

The :focus pseudo-class applies while an element has the focus (accepts keyboard events or other forms of text input).

You could do this:

.hoverclass { border:2px solid red; }

$("div.item").hover(function() {
  $(this).addClass('hoverclass')
},function() {
  $(this).removeClass('hoverclass')
});

This uses .hover(), .addClass() and .removeClass()

like image 21
Manse Avatar answered Sep 21 '22 02:09

Manse


focus() is only usable with form elements and links, and wont work if you try to use it on other types of elements.

See the jQuery doc for focus() for more information,

like image 73
Øyvind Bråthen Avatar answered Sep 22 '22 02:09

Øyvind Bråthen