javascript - After AJAX call, newly loaded CSS selectors not available to jQuery.each() -
note though i'll defer wisdom of herd, don't agree duplicate (at least not linked), , unable find answer problem on linked page.
goal:
- load html element via ajax
- modify css of newly loaded elements based on information in data-attributes.
dilemma:
after finishing (1) above, newly loaded selectors don't seem available. i've been trying variations of jquery.on()
try jquery register these newly added dom elements. don't need event handling; want alter css of of newly arrived elements. i've tried dozen variations on below—most of attempt make style changes within ajax.success()
callback, rather after it; last effort.
this has simple, i'm hour in , jsut can't see it...
here's sample of loaded html:
<div class="jsonprinter-row indented-0 odd-row has-children" data-tab-offset="0" data-key-offset="0"> <div class="jsonprinter-key">category</div> <div class="jsonprinter-list"> <div class="jsonprinter-row indented-1 even-row" data-tab-offset="1" data-key-offset="10"> <div class="jsonprinter-key">key</div> <div class="jsonprinter-value">val</div> </div> </div> </div>
aaaaand js:
var tab_size = 8; var monospace_char = 3; function updateprintedrows(element) { var data = $(element).data(); var width = data['tab-offset'] * tab_size + data['key-offset'] * monospace_char; $(element).css({"padding-left": tostring(width)+"px"}); } // note i've tried both: $(".jsonprinter-row").on("load", function() { updateprintedrows(this); }); // , also: $(document).on("load", ".jsonprinter-row", function() { updateprintedrows(this); }); $("#printsheet").on('click', function() { $("#sheet-view").html( function() { var sheetdata = // points js object $.get("sheet_string.php", {sheet:sheetdata}, function(data) { $("#sheet-view-content").html(data); }); }); });
if event delegation doesn't work load
event, need call function in callback:
$("#printsheet").on('click', function() { $("#sheet-view").html( function() { var sheetdata = // points js object $.get("sheet_string.php", {sheet:sheetdata}, function(data) { $("#sheet-view-content").html(data); $("#sheet-view-content .jsonprinter-row").each(function() { updateprintedrows(this); }); }); }); });
Comments
Post a Comment