在.append()之后编辑DIV

My algorithm:

  1. I get the DIV text from php/SQL ($.get( "read.php"...)
  2. APPEND contenteditable div with loaded text by jQuery
  3. check the div value, if there is errors in text I make div red (assign class, $(this).addClass("fill_red");)
  4. on every change of text - check and assign/remove class if needed.

Problem is: with preloaded text - everything is working. But when I append div using JS - check function don't works. I searched the web, maybe on() method helps me.

But what event?

It should be something like onload, onchange..?

(yes, I could make div generated by php and solve the problem, but I dont want full refresh)

Thank you!

part of code:

//texts load $(function() { $.get( "read.php", function( data ) { var ionka = data.split(' '); ionka.forEach(function(item, i, arr) { var app_text = "<div id=\"segm" + i + "\" contenteditable role=\"textbox\">" + item + "</div>"; $("#textarea").append(app_text); }); }); //checks var intRegex = new RegExp('^[0-9/\]{5}$'); $('#textarea div').each(function(i,elem) { if(!intRegex.test($(this).text())) { $(this).addClass("fill_red"); }else{ $(this).removeClass(); } }); // edit on change. Blur because of contenteditable var segm_array = []; $('#textarea div').each(function(i,elem) { $(this).blur(function() { if (segm_array[i]!=$(this).text()){ segm_array[i] = $(this).text(); if(!intRegex.test(segm_array[i])) { $(this).addClass("fill_red"); }else{ $(this).removeClass();
} } }); });

You dont show much code here, but my guess is that you are trying to add class before new data is loaded into dom

$.get( "read.php" ).done(function( data ) {
    // addClass etc
});