I am building a Django web app with backend. I load the html page dynamically from database and I populate a dynamic table like this, with the delete button in each row predefined and I need to pass a particular cell value to the click event for deletion so i set it to the button id and pass it through a onclick event, but the event is not getting generated and no alert is produced.
I am seeing a lot of examples but i have tried all and even style i added only because of that, i prefer to do it with the <input type="submit" value="Submit"/>
button if possible, but in any case the event is not getting generated.
The code is below: Can anyone please help?
<form name="Dashboard" method="GET" id="AccountForm" action="create">
<script src="https://code.jquery.com/jquery-3.3.1.min.js">
</script>
<script>
$('.btn').on("click", function(event){
alert("I am clicked!");
var id = $(this).attr('id');
});
</script>
<style>
.btn {
border: solid:black;
background-color: inherit;
padding: 14px 28px;
font-size: 16px;
cursor: pointer;
display: inline-block;
}
<table id="tbl">
<tr align="center"><th>Name</th><th>Open</th><th>Test Account</th><th>Display Name</th></tr>
<tr></tr>
{% for i in form.langs %}
<tr align="center">
<td>{{i.name}}</td>
<td>{{i.open}}</td>
<td>{{i.testAccount}}</td>
<td>{{i.displayName}}</td>
<td><input type="button" name="btnDelete" value="Delete" class="btn" id={{i.name}}/></td>
</tr>
{% endfor %}
</table>
</form>
Update: Also is it possible to use this without a .btn class which i am using now? This add-on is answered in the comment given by @freedomn-m below