javascript - accessing html element after it was inserted by DOM manipulation -
this question has answer here:
i have jquery function when button add_entry clicked, inserts 'save button' inside page this; button shows up, assume part working.
$("#add_entry").click(function(){ $("#add_entry").hide(); $('#status').append(<button class="save_entry">test</button>); });
then have yet function supposed called when aforementioned save button clicked:
$(".save_entry").click(function(){ alert("i dont work :/"); });
which makes me wonder...
a) how dom manipulation work? - since jquery part wrapped $document.ready() method mean cant access 'on-the-fly' created elements ?
b) when injecting elements dom manipulation, never appear in source... why?
thank time.
for dynamically added elements, need bind events following.
$(document).on("click", ".save_entry", function(){ alert("i work now"); });
as suggested rory, can bind event #status.
$("#status").on("click", ".save_entry", function(){ alert("i work now"); });
for reference - http://api.jquery.com/on/
Comments
Post a Comment