admin管理员组文章数量:1312817
I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p>
element with id output
with the response value. Now I try to fire an alert every time if the value has changed. I tried to add an event to my <p>
element so I can detect if the variable has changed and fire my function:
<p id="output"></p>
<script>
$("#output").change(function() {
alert("HELLO WORLD!");
});
</script>
There is no alert()
showing up.
I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p>
element with id output
with the response value. Now I try to fire an alert every time if the value has changed. I tried to add an event to my <p>
element so I can detect if the variable has changed and fire my function:
<p id="output"></p>
<script>
$("#output").change(function() {
alert("HELLO WORLD!");
});
</script>
There is no alert()
showing up.
- possibly a duplicate of Is there a jQuery DOM change listener? – apsillers Commented Apr 22, 2016 at 13:56
- Possible duplicate of Listener for property value changes in a javascript object – Daniel Corzo Commented Apr 22, 2016 at 14:10
2 Answers
Reset to default 5Changing the text/HTML value of an element doesn't raise an event. You would need to trigger one manually. For example:
// in the $.ajax success handler:
$('#output').trigger('contentchanged');
// event handler
$("#output").on('contentchanged', function() {
alert("HELLO WORLD!");
});
You could also use a Mutation Observer for this, but note they are not supported in older browsers, such as IE10 and lower.
according to the logic that you described, the content is only changed when ajax was successful. If so, wouldn't the cleanest solution simply be, in your ajax success, run the function that you are expecting to run on content change?
本文标签: javascriptDetect variable change with jqueryStack Overflow
版权声明:本文标题:javascript - Detect variable change with jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741903691a2404016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论