Reputation: 1775
I have span elements that are dynamically generated from an ajax response. I am trying to detect a change to the span value using the below code and it seems to work in jsfiddle with fixed elements but not in a real scenario where data is dynamically generated. The span values are updated with setInterval function number increment which i am trying to detect.
The steps i am taking are - would appreciate some advice on why my code would not be working?
$('[id*="minutes"]').each(function() {
spanid = $(this).attr('id');
console.log(spanid);
$("#"+spanid).on('change',function(){
spanval = $(this).text();
id = $(this).attr('id').charAt(0);
if(spanval > 00) {
$('#results').text(span);
$("#"+id+"-wt-html").css({"background":"#FFE95B", "color":"red"});
} else {
$("#"+id+"-wt-html").removeAttr("style")
}
});
});
sample HTML
<td id="9-wt-html">
<div id="9-wt-ajax">
<span id="9-hours">00</span>:
<span id="9-minutes">15</span>:
<span id="9-seconds">12</span>
</div>
</td>
The above span elements are created before the ajax call using a fixed script below:
for (var key in skills_arr) {
//console.log(skills_arr[key]+" - "+key);
$('#Table > tbody:last-child').append('<tr><td>'+skills_arr[key]+'</td><td id="'+key+'-cw-html"><div id="'+key+'-cw-ajax"></div></td><td id="'+key+'-aa-html"><div id="'+key+'-aa-ajax"></div></td><td id="'+key+'-wt-html"><div id="'+key+'-wt-ajax"><span id="'+key+'-hours">00</span>:<span id="'+key+'-minutes">00</span>:<span id="'+key+'-seconds">00</span></div></td><td id="'+key+'-op-html"><div id="'+key+'-op-ajax"></div></td></tr>');
}
Upvotes: 0
Views: 1604
Reputation: 12022
Okay, I have created a snippet
below based on my understandings.
Also, would like to highlight the below points.
For
span
element, we can't listen for thechange
event. Instead, the code below will listen for theDOMSubtreeModified
event.Also, we can directly listen for the event of the element using
$('[id*="minutes"]').on('DOMSubtreeModified', function(){})
if the element exists before this line.Otherwise, we have to listen the same from its
parent
ordocument
as given below.
$('#Table').on('DOMSubtreeModified', '[id*="minutes"]', function() {
var span = $(this);
var spanValue = parseInt(span.text());
var id = span.attr('id').charAt(0);
if(spanValue > 0) {
//$('#results').text(span); // Not sure what is this?
$("#"+id+"-wt-html").css({"background":"#FFE95B", "color":"red"});
} else {
$("#"+id+"-wt-html").removeAttr("style")
}
});
var skills_arr = {
'9': 'some value here'
};
for (var key in skills_arr) {
$('#Table > tbody:last-child').append('<tr><td>'+skills_arr[key]+'</td><td id="'+key+'-cw-html"><div id="'+key+'-cw-ajax"></div></td><td id="'+key+'-aa-html"><div id="'+key+'-aa-ajax"></div></td><td id="'+key+'-wt-html"><div id="'+key+'-wt-ajax"><span id="'+key+'-hours">00</span>:<span id="'+key+'-minutes">00</span>:<span id="'+key+'-seconds">00</span></div></td><td id="'+key+'-op-html"><div id="'+key+'-op-ajax"></div></td></tr>');
}
setTimeout(function(){
$('#9-minutes').text('15');
$('#9-seconds').text('12');
}, 3000);
setTimeout(function(){
$('#9-minutes').text('00');
$('#9-seconds').text('10');
}, 6000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="Table">
<tbody>
</tbody>
</table>
<p>Results:</p>
<div id="#results">
</div>
Upvotes: 1
Reputation: 9662
The main reason it might not be working is because you are enclosing divs inside td. You need to enclose them in div to work:
<div id="9-wt-html">
<div id="9-wt-ajax">
<span id="9-hours">00</span>:
<span id="9-minutes">15</span>:
<span id="9-seconds">12</span>
</div>
</div>
Then in your jQuery code you need to transform span text to integre to work:
$('[id*="minutes"]').each(function() {
spanid = $(this).attr('id');
console.log(spanid);
$("#" + spanid).on('change', function() {
spanval = parseInt($(this).text());
id = $(this).attr('id').charAt(0);
if (spanval > 00) {
//$('#results').text(span); // ?where is span variable?
$("#" + id + "-wt-html").css({
"background": "#FFE95B",
"color": "red"
});
} else {
$("#" + id + "-wt-html").removeAttr("style")
}
});
});
Here is a JSFiddle
Upvotes: 0