title: Jquery favorite function call api with ajax
body: <p>I'm trying to create a favorite function in my django template that let user click on the icon and it will call to an api i made to send add and remove that favorite item from user data. </p>
  
  <p><a href="https://i.stack.imgur.com/G6L5I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/G6L5I.png" alt="favorite and unfavorite button"></a></p>
  
  <p>Currently how it work is if user click on the icon(favorite or unfavorite) it will call to an api that handle add favorite or remove favorite, and also it will replace the DOM of that  tag with the opposite (for example if i click on favorite then the entire  tag of it will be replace with  tag content that has unfavorite icon and onclick function)</p>
  
  <p>here my html for the  tag, it display base on if it is favorite or not( {% %} tag is django handling it ):</p>
  
  <pre><code>&lt;div class="article-category"&gt;{{ property.get_type_display }}
       &lt;div class="bullet"&gt;&lt;/div&gt; {{ property.publish_date|date:'d-m-Y' }}
       {% if not request.user|is_favorite:property.id %}
           &lt;a id="mylink" href="javascript:onclickFunction()" value="{{ property.id }}"&gt;
              &lt;i class="far fa-heart fa-lg" style="color: red" title="Add to favorite"&gt;&lt;/i&gt;
           &lt;/a&gt;
        {% else %}
            &lt;a id="mylink2" href="javascript:onclickFunction()" value="{{ property.id }}"&gt;
               &lt;i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite"&gt;&lt;/i&gt;
            &lt;/a&gt;
        {% endif %}
   &lt;/div&gt;
  </code></pre>
  
  <p>And here is my jquery script:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
              $('#mylink').on('click', function (event) {
                  event.preventDefault();
                  property_id = $(this).attr("value")
                  $.ajax({
                      type: "POST",
                      url: "http://localhost:9999/api/add_favorite/" + property_id + "/",
                      beforeSend: function (xhr) {
                          xhr.setRequestHeader('Authorization', 'Bearer {{ refresh_token }}');
                      },
                      success: function (data) {
                          if (data.code == 200) {
                              alert('ok');
                              replace_part_1 = '&lt;a id="mylink2" href="javascript:onclickFunction()" value="' + property_id +'"&gt;&lt;i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite&gt;&lt;/i&gt;&lt;/a&gt;'
                              $("#mylink").replaceWith(replace_part_1);
                          }
                      }
                  });
                  return false;
              });
              $('#mylink2').on('click', function (event) {
                  event.preventDefault();
                  property_id = $(this).attr("value")
                  $.ajax({
                      type: "DELETE",
                      url: "http://localhost:9999/api/remove_favorite/" + property_id + "/",
                      beforeSend: function (xhr) {
                          xhr.setRequestHeader('Authorization', 'Bearer {{ refresh_token }}');
                      },
                      success: function (data) {
                          if (data.code == 200) {
                              alert('ok');
                              replace_part_2 = '&lt;a id="mylink" href="javascript:onclickFunction()" value="' + property_id +'"&gt;&lt;i class="far fa-heart fa-lg" style="color: red" title="Add to favorite"&gt;&lt;/i&gt;&lt;/a&gt;'
                              $("#mylink2").replaceWith(replace_part_2);
                          }
                      }
                  });
                  return false;
              });
          });
  &lt;/script&gt;
  </code></pre>
  
  <p>Now the first time i click on favorite or unfavorite it sent to the api and work, the replace html part for "#mylink2" onclick event but the "#mylink" replace doesn't include the  tag which contain the icon.</p>
  
  <p>After this any clicking event after that won't work and i have to refresh the page to click it again to work. Any click event after that also return this error:</p>
  
  <blockquote>
    <p>Uncaught ReferenceError: onclickFunction is not defined
        at :1:1</p>
  </blockquote>
  
  <p>I'm super noob at jquery so i can't seem to find what is wrong, hope someone can help me</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>i changed my script to this by replace with value attribute of anchor tag:</p>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function () {
              $('.article-details').on('click', '#mylink', function(event) {
                  event.preventDefault();
                  property_id = $(this).attr("value")
                  $.ajax({
                      type: "POST",
                      url: "http://localhost:9999/api/add_favorite/" + property_id + "/",
                      beforeSend: function (xhr) {
                          xhr.setRequestHeader('Authorization', 'Bearer {{ refresh_token }}');
                      },
                      success: function (data) {
                          if (data.code == 200) {
                              alert('ok');
                              replace_part_1 = '&lt;a id="mylink2" href="#" value="' + property_id +'"&gt;&lt;i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite&gt;&lt;/i&gt;&lt;/a&gt;'
                              $("a[value='" + property_id + "']").replaceWith(replace_part_1);
                          }
                      }
                  });
                  return false;
              });
              $('.article-details').on('click', '#mylink2', function(event) {
                  event.preventDefault();
                  property_id = $(this).attr("value")
                  $.ajax({
                      type: "DELETE",
                      url: "http://localhost:9999/api/remove_favorite/" + property_id + "/",
                      beforeSend: function (xhr) {
                          xhr.setRequestHeader('Authorization', 'Bearer {{ refresh_token }}');
                      },
                      success: function (data) {
                          if (data.code == 200) {
                              alert('ok');
                              replace_part_2 = '&lt;a id="mylink" href="#" value="' + property_id +'"&gt;&lt;i class="far fa-heart fa-lg" style="color: red" title="Add to favorite"&gt;&lt;/i&gt;&lt;/a&gt;'
                              $("a[value='" + property_id + "']").replaceWith(replace_part_2);
                          }
                      }
                  });
                  return false;
              });
          });
  &lt;/script&gt;
  </code></pre>
  
  <p>and my html to:</p>
  
  <pre><code>&lt;div class="article-category"&gt;{{ property.get_type_display }}
                                  &lt;div class="bullet"&gt;&lt;/div&gt; {{ property.publish_date|date:'d-m-Y' }}
                                  {% if not request.user|is_favorite:property.id %}
                                  &lt;a id="mylink" href="#" value="{{ property.id }}"&gt;
                                      &lt;i class="far fa-heart fa-lg" style="color: red" title="Add to favorite"&gt;&lt;/i&gt;
                                  &lt;/a&gt;
                                  {% else %}
                                  &lt;a id="mylink2" href="#" value="{{ property.id }}"&gt;
                                      &lt;i class="fas fa-heart fa-lg" style="color: red" title="Remove from favorite"&gt;&lt;/i&gt;
                                  &lt;/a&gt;
                                  {% endif %}
                              &lt;/div&gt;
  </code></pre>
  
  <p>it worked but the onclick of #mylink when changing the html the i tag inside  still disappear(the #mylink2 onclick worked it changed the html white the  tag)</p>