Remove the .active class from all the items:
$('.item').removeClass('active');
And then move the active class to the item that you just created with the anchor. E.g.:
$('.carousel-inner').append(`...
`);
This works at least for Bootstrap 4:
https://jsfiddle.net/gal007/kt3jLopz/20/