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>&nbsp;</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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -