I'm trying to have dynamically generated elements call functions, or be selected by jquery to do another action. Not having much luck so far.
html
<button id="staticButton">static button</button>
<button id="onClickButton" onclick="onClickFunction()">onClick button</button>
<div id="from-static"><h3>buttons created using static button</h3></div>
<div id="from-dynamic"><h3>buttons created using dynamic buttons</h3></div>
js
element_number = 1;
$('#staticButton').on('click', function(){
$('#from-static').append(
'<button id=' + element_number + ' >' + 'Dynamic Button #' + element_number + ' generated by static button</button> <br/>'
);
element_number = element_number + 1;
});
how would I select the newly generated buttons?
$('?').on('click', function(){
var currentId = $(this).attr('id');
$('#from-dynamic').append(
'<button id=' + element_number + ' >' + 'Dynamic Button #' + element_number + ' generated by Button #: ' + currentId + '</button> <br/>'
);
element_number = element_number + 1;
});
another approach perhaps: add an onclick event to generated buttons that call a function
function onClickFunction(){
alert("should this work?");
}
but clicking on the button I get: ReferenceError: onClickFunction is not defined