I am using jQuery v3.6.0 on a page that should dynamically allow a user to bookmark/unbookmark an item displayed on the page.
Here is my code snippet:
HTML
<div id="37.3" class="social-info-container">
   <div id="VK7n" class="social bookmarkable">
    <i title="Bookmarks" class="fa fa-bookmark"></i>0
   </div>
</div>
Javascript
function bookmarkItem(id, elem_id){
  let [ctid, oid] = id.split('.');
    console.log(`Posting bookmark for item with content_type: ${ctid}, object_id: ${oid}`);
    $.ajax({
        type: "POST",
    headers:{'X-CSRFToken': special_csrf_token },
        url: social_bookmark_url,
        data: {
            cid: ctid,
            oid: oid,
      note: ''
        },
        dataType: "json",
        success: function(resultData) {
      console.log(`Reponse ok: ${resultData.ok}`);
      console.log(`Elem id: ${elem_id}`);
      $(`div#${elem_id}.social.bookmarkable > i.fa.fa-bookmark`).toggleClass('bookmarked');
      
      let orig_html = $(`div#${elem_id}`).html();
      let old_count = $(`div#${elem_id}`).text();
      let new_count = resultData.count; 
      let new_html = orig_html.replace(old_count,new_count)
      console.log(old_count);
      console.log(orig_html);
      console.log(new_count);
      console.log(new_html);
      $(`div#${elem_id}`).html(new_html);
        },
        error: function(xhr, status, error) {
            if (xhr.status == 403){
        window.location.href = login_url;
      }
      else {
        alert(`Something went wrong: ${xhr.statusText}`);
      }
        }
    });  
}
$().ready(function(){
    $('.social.bookmarkable .fa.fa-bookmark').on('click', function(e) {
      alert('Clicked!');
      let elem_id = $(this).closest('div.social.bookmarkable').attr('id');
      console.log(`Elem id (1): ${elem_id}`);
      let id = $(this).closest('div.social-info-container').attr('id');
      bookmarkItem(id, elem_id);
    });
});
When I click the bookmark icon, it works ONCE - after that I have to refresh the page to get it to work again. I thought using the on() method to bind to the click event would avoid this problem.
Why is the event being triggered just once - and how do I fix this?
 
    