Reputation: 43
The idea for this page is to load the 'skills' from my java application and dynamically create a Delete button that allows me to delete those 'skills' from the application.
Error received when attempting to click the button to delete a skill.
manageTech:1 Uncaught ReferenceError: deleteMe is not defined at HTMLButtonElement.onclick (manageTech:1)
js
file:
$( document ).ready( () => {
var url = window.location;
// GET REQUEST
$("#btnGetFiles").click( (event) => {
event.preventDefault();
ajaxGet();
});
function deleteMe(id) {
window.alert("Button clicked, id " + id + ", text");
$.ajax({
url: '/api/technologyList' + '/' + id,
type: 'DELETE',
success: function() {
ajaxGet();
}
})
}
// DO GET
function ajaxGet() {
$.ajax({
type: 'GET',
url: '/api/technologyList',
success: function (skills) {
$.each(skills, function (i, skill) {
$('#listFiles').append('<tr>' + '<td>' + '<button ' +
'type="submit" ' +
'data-id="' + skill.id + '" ' +
'onclick="deleteMe('+skill.id+')"' +
'name="deletebtn">' +
'Delete' +
'</button>' + '</td>' +
'<td>' + skill.id + '</td>' +
'<td>' + skill.logo + '</td>' +
'<td>' + skill.techName + '</td></tr>')
})
}
});
}
});
Upvotes: 1
Views: 1368
Reputation: 370789
Your deleteMe
is not on the top level - it's inside your $( document ).ready( () => {
, so it's not a global variable, so the inline handler onclick="deleteMe('+skill.id+')"
fails. (Inline handlers can only reference global variables on the top level.)
Although you could fix it by moving deleteMe
outside, inline handlers are bad practice; consider attaching an event listener properly using Javascript instead. Remove the inline handler, and use event delegation:
$('#listFiles').on('click', 'button[name="deletebtn"]', function() {
deleteMe(this.dataset.id);
});
Upvotes: 3