Home > Web Front-end > JS Tutorial > How Can I Trigger Events on Disabled Input Elements in Browsers Like Firefox?

How Can I Trigger Events on Disabled Input Elements in Browsers Like Firefox?

Patricia Arquette
Release: 2024-12-19 08:40:16
Original
829 people have browsed it

How Can I Trigger Events on Disabled Input Elements in Browsers Like Firefox?

Workaround for Events on Disabled Input Elements

Disabled input elements typically do not trigger mouse events. While most browsers propagate events from disabled elements to their parent containers, Firefox does not support this behavior. This can pose challenges for certain scenarios.

Problem:

You need to handle events on disabled input elements, but the default behavior prevents this.

Solution:

To overcome this issue, a technique can be employed to intercept events from a disabled input element and trigger a click on an adjacent element that is not disabled.

Here's an example implementation using HTML and jQuery:

<div>
Copy after login
$("div > div").click(function (evt) {
    $(this).hide().prev("input[disabled]").prop("disabled", false).focus();
});
Copy after login

In this example, the div element overlaps the disabled input element. When the div is clicked, it triggers a click on the input element, which in turn enables the input element and sets focus on it.

Example:

Visit the following JSFiddle example to see this technique in action:

https://jsfiddle.net/RXqAm/170/

The above is the detailed content of How Can I Trigger Events on Disabled Input Elements in Browsers Like Firefox?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template