Is there a way to add a new li
element to the ul
on the right side? Each card comes with a ul
and a form
.
After submitting the form, I want to append a new li
to the corresponding ul
. Each ul
has a class of .topics
. However, the new li
is currently being added to all cards.
I am struggling with finding a solution to append the li inside the correct ul where it was created.
This shows my HTML structure
<div class="card" id="card1">
<div class="card-body">
<ul class="topics">
<li>....</li>
<li>....</li>
<li>....</li>
</ul>
</div>
<div class="card-footer">
<form method="post" class="topic-form">
....
</form>
</div>
</div>
<div class="card" id="card2">
<div class="card-body">
<ul class="topics">
<li>....</li>
<li>....</li>
<li>....</li>
</ul>
</div>
<div class="card-footer">
<form method="post" class="topic-form">
....
</form>
</div>
</div>
This displays my JS code
$('form.topic-form').submit( function (e) {
e.preventDefault();
var form = $(this)[0];
$.ajax({
...ajax stuff...
},
success: function (json) {
form.reset();
$(".topics").append('<li>.....</li>')
}
}
}