Here is where I want to add the html
<div id="people">
This is my script and when the button is clicked its text will be change
<script>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "response1.php",
dataType: "json",
success: function(JSONObject) {
var peopleHTML = "";
// Loop through Object and create peopleHTML
for (var key in JSONObject) {
if (JSONObject.hasOwnProperty(key)) {
peopleHTML += "<div class='col-lg-3 col-md-3 col-xs-3'
style='padding-top:10px;'>";
peopleHTML += "<div class='box'>";
peopleHTML += "<div class='row'>";
peopleHTML += "<h3>" + JSONObject[key]["name"] + "</h3>";
peopleHTML += "<div class='col-xs-12'>";
peopleHTML += "<p>"+JSONObject[key]["gender"]+"</p>";
peopleHTML += "</div>";
peopleHTML += "<button class='btn btn-primary' id='cris'>
CLICK ME</button>";
peopleHTML += "</div>";
peopleHTML += "</div>";
peopleHTML += "</div>"; }
}
// Replace table’s tbody html with peopleHTML
$("#people").append(peopleHTML);
$('#cris').click(function(){
event.preventDefault();
$(this).text('I CHANGE');
});
}
});
});
</script>
How can I access the html elements I added so that the click function will work? I also used json to get my data's on database.
$('#cris').click(...)will work just fine. The issue may be thateventis not defined.