Home  >  Article  >  Web Front-end  >  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
Mary-Kate OlsenOriginal
2024-10-21 18:33:29717browse

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>
<code class="html"><span id="valBox"></span>
<input type="range" min="5" max="10" step="1" oninput="showVal(this.value)"></code>

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>

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!

Statement:
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