javascript - How to Select HTML Elements Added Using .html() in jQuery -
i attempting make small browser game , having trouble selecting elements added using .html() in jquery.
for game, use html template system templates stored in templates/%name%.txt. then, load template, use following function.
function template(name) { $.get("templates/" + name + ".txt", function(data) { $(".content").html(data); }); } here example of template use:
<div class="title"> <!-- fill --> </div> <table> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> </table> <button class="in-btn quit">quit game</button> <button class="in-btn reset">reset game</button> now, in order me edit title of game when changes occur, use $(".title").html("game - <span>player data example</span>");.
however, because template not part of html on page, cannot select using regular selectors, $(".title") returns no elements.
so, how select element added using .html() via jquery?
there way handle events where, example, <td> has been added using .html():
$(document).on('click', 'td', function(){ // something... });
why don't use $(".content").load("templates/" + name + ".txt") load?
and can still select , add listener using delegation. can perform after view gets populated if pass callback second argument of load(..., <callback>).
and add more if content there in dom (dosen't matter how itsadded, html(..) or dom appendchild method) can select (at time of presence).
Comments
Post a Comment