javascript - jQuery added form elements cannot be selected -
i apologise description if it's not fitting. hard describe what's going on.
i have set jsfiddle here.
function addnewform() { $('body').html(function () { //display log in form return "<input type=\"text\" placeholder=\"word or phrase\" id=\"prompt\" />\n" + "<textarea id=\"answer\" placeholder=\"answer text\"></textarea>\n" + "<input type=\"button\" id=\"addnew\" value=\"add this!\" />\n" + "<p> </p>"; }); } $("#addnew").click(function () { $("p").html("add new clicked<br />"); }); $("a").click(function () { addnewform(); });
is syntax correct? reason, $("#addnew").click
works when generated outside of own function button stops working since have tidied code , placed within own function. there reason jquery may not recognise element has been created through function?
cheers!
the solution here :
https://jsfiddle.net/wvepd6ge/7/
when add button, need attach event :
function addnewclicked() { $('.result').html("add new clicked<br/>"); } $("a").click(function () { addnewform(); $("#addnew").click(function () { addnewclicked(); }); });
Comments
Post a Comment