admin管理员组

文章数量:1313599

I'm trying to trigger an event handler when a script modifies an input element (text field.) On Internet Explorer, I can use the onpropertychange event, but on Firefox and other browsers there is no such event. So, according to the W3C docs, it seems the DOMAttrModified event does exactly what I want. But it doesn't fire in Firefox 11.

Here's a simple code snippet which reproduces the problem. I have an input text field, and a button which adds a character to the value attribute of the input text field. Clicking on the add char button should cause the DOMAttrModified event to fire:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            function addChar() {
                var q = document.getElementById("query");
                q.value += "X";
            }

            function loadevents() {
                var q = document.getElementById("query");
                q.addEventListener("DOMAttrModified", function() {alert("DOMAttrModified event!");
                }, false);
            }
        </script>
    </head>
    <body onLoad="loadevents()">
        <input type="text" id="query">
        <br>
        <input type="submit" value="add char" onclick="addChar()">
    </body>
</html>

But it doesn't. Any idea what I'm doing wrong? (I know that DOM Level 3 deprecates this event, but there doesn't seem to be a viable alternative right now. As far as I know, Firefox 11 still supports it.)

I'm trying to trigger an event handler when a script modifies an input element (text field.) On Internet Explorer, I can use the onpropertychange event, but on Firefox and other browsers there is no such event. So, according to the W3C docs, it seems the DOMAttrModified event does exactly what I want. But it doesn't fire in Firefox 11.

Here's a simple code snippet which reproduces the problem. I have an input text field, and a button which adds a character to the value attribute of the input text field. Clicking on the add char button should cause the DOMAttrModified event to fire:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            function addChar() {
                var q = document.getElementById("query");
                q.value += "X";
            }

            function loadevents() {
                var q = document.getElementById("query");
                q.addEventListener("DOMAttrModified", function() {alert("DOMAttrModified event!");
                }, false);
            }
        </script>
    </head>
    <body onLoad="loadevents()">
        <input type="text" id="query">
        <br>
        <input type="submit" value="add char" onclick="addChar()">
    </body>
</html>

But it doesn't. Any idea what I'm doing wrong? (I know that DOM Level 3 deprecates this event, but there doesn't seem to be a viable alternative right now. As far as I know, Firefox 11 still supports it.)

Share Improve this question edited Mar 27, 2022 at 13:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 2, 2012 at 17:25 Channel72Channel72 24.7k35 gold badges115 silver badges187 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Changing the value in an input doesn't fire the DOMAttrModified event, that's all..

You need to change the attribute of the input node, not the property of the variable.
It's like the difference between the two jQuery functions: .prop and .attr

Read:

  • Which HTMLElement property change generates DOMAttrModified?
  • this forum discussion

(repeating my answer from Which HTMLElement property change generates DOMAttrModified? here, because it's relevant to this question):

Please also note that NO DOMAttrModified events will be fired when the 'disabled' attribute is set. So if your event is not firing, that could be the reason. This also goes for the IE-only 'onPropertyChange' event.

本文标签: javascriptDOMAttrModified doesn39t fireStack Overflow