I'm currently building a small alarm application with Vue.js. I want to have an eventListener on buttons with a class of "del" that calls a method and hands over the event, I'm using Vue's "mounted" feature for that:
mounted: function timeInterval () {
var app = this;
var del = document.getElementsByClassName("del");
del.addEventListener('click', function (e) {
app.deleteAlarm(e);
},
}
In that method I want to get the id of the button that was clicked and do something with it:
deleteAlarm: function (e) {
var app = this;
var id = e.target.id;
app.alarms.splice(id, 1);
}
I spent hours on figuring out what's going wrong but I can't get it.
Edit: The way I want to do this is important, because the buttons are part of a dynamic list, that gets rendered via v-html. This is the method that adds the HTML to the data variable:
getAlarmList: function () {
var app = this;
app.alarmTable = '';
for (let i=0; i<app.alarms.length; i++) {
app.alarmTable += "<tr><td>"+app.alarms[i][0]+"</td><td>"+app.alarms[i][1]+":"+app.alarms[i][2]+":"+app.alarms[i][3]+"</td><td><button type=\"button\" id=\""+i+"\" class=\"btn btn-dark btn-sm del\">Löschen</button></td></tr>";
}
And this is how the variable gets rendered out with the v-html directive:
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Time</th>
<th></th>
</tr>
</thead>
<tbody v-html="alarmTable">
</tbody>
</table>
alarms?