Have a PREVIEW BUTTON. Clicking it initiates a function that generates new buttons with data attributes through ajax, appending to other html. Want to get the attribute value of "data-id" of the generated EDIT BUTTON, but the value comes up as "undefined".
var answer_id;
$(document).on('click', '#preview', function(){//PREVIEW button clicked
...some code...
$.ajax({...});
//ajax retrieves data and appends to html code
//this data contains buttons. One of the buttons is:
//<button id="edit" type="button" class="btn" value="edit" data-id="521">Edit</button>
answer_id = $('#edit').attr("data-id");
alert(answer_id);
});
I can inspect the browser page code and the EDIT BUTTON has the data-id value as expected. However, the jquery alert that comes up says "undefined".
I have tried:
$('#edit').attr("data-id")
$("#edit").data("id")
$("#edit").data().value;
What is the error?