I am using the .Append() method of JQuery to append buttons to an unordered list, and for some reason the appended button will not call its function when clicked. I inserted the same button using HTML, and it worked fine, but for some reason the appended button will not run. Is there a way to get them to work, or do I just have to work around it?
$("#requestDisplay").append(<button class='requestRemovalButton'>" + "Remove Request" + "</button>);
Vs in the HTML
<button class="requestRemovalButton">Remove Request</button>
The latter will run the function, however the former will not work, here is the function I was using to test the buttons.
$(".requestRemovalButton").on( "click", remove);
function remove() {
alert( "The request is being removed, please wait." );
}
Thanks for any advice you may have.
.on()will only add the event listener to.requestRemovalButtonelements that exist at the point you call.on()..append("<button class='requestRemovalButton'>" + "Remove Request" + "</button>");(so there's a quote before the opening<buttonand after the</button>)