jquery - dynamically detect change to span element -


i have span elements dynamically generated ajax response. trying detect change span value using below code , seems work in jsfiddle fixed elements not in real scenario data dynamically generated. span values updated setinterval function number increment trying detect.

the steps taking - appreciate advice on why code not working?

  1. for elements id containing "minutes" <- these span elements
  2. get id's
  3. detect change in elements id's step 2
  4. when change detected, span element value
  5. check if span value greater 00 i.e. 01 (this minute value)
  6. if condition step 5 met apply css
  7. if condition step 5 not met remove css
$('[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 created before ajax call using 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>'); } 

okay, have created snippet below based on understandings.

also, highlight below points.

for span element, can't listen change event. instead, code below listen domsubtreemodified event.

also, can directly listen event of element using $('[id*="minutes"]').on('domsubtreemodified', function(){}) if element exists before line.

otherwise, have listen same parent or document 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 this?                $("#"+id+"-wt-html").css({"background":"#ffe95b", "color":"red"});      } else {              $("#"+id+"-wt-html").removeattr("style")      }  });    var skills_arr = {    '9': 'some value here'  };    (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>


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -