Home > Web Front-end > JS Tutorial > Why is the onchange Event Not Triggered for Range Input Drag in Firefox?

Why is the onchange Event Not Triggered for Range Input Drag in Firefox?

Mary-Kate Olsen
Release: 2024-10-21 18:33:29
Original
806 people have browsed it

Why is the onchange Event Not Triggered for Range Input Drag in Firefox?

Firefox onchange Event Not Triggered on Range Input Drag

In input elements with type "range," when the slider is dragged, the onchange event is only triggered when the slider is dropped to a new position in Firefox. In contrast, Chrome and other browsers trigger onchange events during the drag.

Solution: Use oninput Event

Firefox correctly triggers the onchange event only upon release, as per the specification. To capture live updates during dragging in all browsers, use the oninput event instead.

<code class="html">function showVal(newVal){
    document.getElementById("valBox").innerHTML=newVal;
}</code>
Copy after login
<code class="html"><span id="valBox"></span>
<input type="range" min="5" max="10" step="1" oninput="showVal(this.value)"></code>
Copy after login

Combining oninput and onchange for Cross-Browser Compatibility

For cross-browser compatibility, consider combining both oninput and onchange event handlers:

<code class="html"><span id="valBox"></span>
<input
  type="range"
  min="5"
  max="10"
  step="1"
  oninput="showVal(this.value)"
  onchange="showVal(this.value)"
/></code>
Copy after login

This ensures that onchange events are still triggered in Firefox upon release, while oninput events provide continuous updates in all browsers.

The above is the detailed content of Why is the onchange Event Not Triggered for Range Input Drag in Firefox?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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