Reputation: 31
I don't get it. Shouldn't this code work?
Here the HTML-Code
<div id="past">
<div data-rollid="283585" class="ball ball-1">7</div>
<div data-rollid="283586" class="ball ball-1">4</div>
<div data-rollid="283587" class="ball ball-8">11</div>
<div data-rollid="283588" class="ball ball-8">14</div>
<div data-rollid="283589" class="ball ball-1">2</div>
<div data-rollid="283590" class="ball ball-1">5</div>
<div data-rollid="283591" class="ball ball-1">2</div>
<div data-rollid="283592" class="ball ball-1">6</div>
div data-rollid="283593" class="ball ball-8">13</div>
<div data-rollid="283594" class="ball ball-1">3</div>
</div>
And here the jQuery-Code
$('#past').one('DOMSubtreeModified', function() {
var stuff = $('#panel8-14 .mytotal').html();
var test = '-';
if( test.indexOf(stuff) >= 0){
console.log('asdf');
}
else{
console.log('nope');
}
});
The Problem is:
When i do it with this code and use the .one() function then it calls the function only once at the first loading and not when the div content changes...
When i do it with .bind or with .on then i gives it back 2 times...
Upvotes: 1
Views: 3256
Reputation: 1
You can use MutationObserver
var target = $("#past").get(0);
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.removedNodes.length) {
console.log("removed nodes", mutation.removedNodes[0].nodeValue);
alert("removed nodes");
}
if (mutation.addedNodes.length) {
alert("added nodes");
console.log("added nodes", mutation.addedNodes[0].nodeValue);
}
});
});
var config = {
childList: true,
subtree: true,
characterData: true
};
observer.observe(target, config);
$("#past div:first").text(1); // do stuff
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="past">
<div data-rollid="283585" class="ball ball-1">7</div>
<div data-rollid="283586" class="ball ball-1">4</div>
<div data-rollid="283587" class="ball ball-8">11</div>
<div data-rollid="283588" class="ball ball-8">14</div>
<div data-rollid="283589" class="ball ball-1">2</div>
<div data-rollid="283590" class="ball ball-1">5</div>
<div data-rollid="283591" class="ball ball-1">2</div>
<div data-rollid="283592" class="ball ball-1">6</div>
<div data-rollid="283593" class="ball ball-8">13</div>
<div data-rollid="283594" class="ball ball-1">3</div>
</div>
Upvotes: 5