Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire onmouseover event when element is disabled

I have some controls that I need to disable when users don't have edit privileges, but are sometimes not wide enough to show the entire text of the selected option element. In which case I've added a tool tip with ASP.NET and the following code

ddl.Attributes.Add("onmouseover", "this.title=this.options[this.selectedIndex].title")

This works when the control is enabled, but doesn't work when it is disabled.

The following alert will not fire when a mouse is over the select element:

<select disabled="disabled" onmouseover="alert('hi');">
    <option>Disabled</option>
</select>

See this fiddle.

Q: Can I fire the onmouseover event for controls that are disabled?

like image 516
KyleMit Avatar asked Aug 07 '13 21:08

KyleMit


People also ask

When would the onmouseover event handler be triggered by a user?

The Onmouseover event handler in Javascript is an event handler that is triggered when a user places the cursor of a mouse over an element. This can be any HTML element, such as a link, an image, a paragraph, a header, etc.

How do I disable onmouseover?

Solution: To disable the display of hover text for a link, perform the following steps: From the Service Desk tab, click on the View pull-down and then click on Preferences. In the General settings, check the option "Disable Mouseover Previews" as shown in Figure 1.

Is onmouseover an event handler attribute?

The onmouseover event attribute works when the mouse pointer moves over the specified element. Attribute value: This attribute contains single value script which works when mouse moves over the element.

What is onmouseover and Onmouseout?

Definition and UsageThe onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.


2 Answers

Disabled elements do not fire events, e.g. users cannot hover or click them to trigger a popover (or tooltip). You can however wrap the disabled element with a DIV and listen to the event fired on that element instead.

like image 61
Diodeus - James MacFarlane Avatar answered Oct 13 '22 13:10

Diodeus - James MacFarlane


Update: Please see nathan william's comment for some serious limitations to this approach. I've updated the fiddle to illustrate the problem areas more clearly.


Expanding on what @Diodeus said, you can use jQuery to automatically create the div container for you and wrap it around any disabled elements.

  1. Use the :disabled selector to find all disabled elements.
  2. Then call the .wrap() method with a function callback
  3. You can use this to refer to the current element in the set.
  4. Then use .attr() method to get the onmouseover value from the parent element and apply the same value to the new div.
$(':disabled').wrap(function() {
    return '<div onmouseover="' + $(this).attr('onmouseover') + '" />';
});

Demo in jsFiddle

like image 27
KyleMit Avatar answered Oct 13 '22 12:10

KyleMit