Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically trigger browser's default hover effect on HTML input

Consider:

<input type="radio" id="a"/>
<label for="a">Hello</a>

When you mouse over the radio button or the label, the radio button gets highlighted. Different browsers highlight it differently, but it looks like a default behavior. Now let's say there is a

<div id="bla">blabla</div>

somewhere else on the page. Is there any way to trigger that default highlight of the radio button when mousing over the div#bla?

EDIT: To clarify, I was looking to "trigger" a native ":hover" pseudo-class of an element, which is not possible. Spec

like image 576
Dimskiy Avatar asked May 19 '15 16:05

Dimskiy


People also ask

What is hover effect in html?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.

How do you hover text in HTML?

There are two ways you can create a hover text for your HTML elements: Adding the global title attribute for your HTML tags. Creating a tooltip CSS effect using :before selector.

What is&: hover in CSS?

The :hover CSS pseudo-class matches when the user interacts with an element with a pointing device, but does not necessarily activate it. It is generally triggered when the user hovers over an element with the cursor (mouse pointer).

What is the hover effect?

What is a CSS Hover Effect? A CSS hover effect takes place when a user hovers over an element, and the element responds with transition effects. It is used to mark the key items on the web page and it's an effective way to enhance the user experience.


2 Answers

JUST FOR INFO

to normalize your hover effect through different browsers:

input[type='radio']:hover{
//add your css here
}

you can also use :active, :checked, :before, :after to add more styles to it.

ANSWER TO YOUR QUESTION

Your question requires to handle the hover effect with some javascript.

$('#bla').hover(function(){
  $(":radio").css(//add your rules here);
});

EDIT:

My solution requires using CSS. What you want to get is to add a pseudo class (:hover) to an element. This is not possible. See this SO question for further details.

like image 99
Lelio Faieta Avatar answered Oct 06 '22 00:10

Lelio Faieta


From the spec:

More than one label may be associated with the same control by creating multiple references via the for attribute

Given that, you could just turn your div into another label to achieve exactly what you want without the need for any CSS or JavaScript.

Note that, if this new label is not a descendant of the input element's form then you should use its form attribute to specify the ID of the form

Of course, if you don't want focus to be transferred to the input element when clicking on the second label then you'd need a little bit of JavaScript but I wouldn't recommend doing that.

like image 26
Shaggy Avatar answered Oct 06 '22 00:10

Shaggy