title: Unable to replace text with replaceWith in jquery
body: <p>I have favorite script which add data to <code>SQL</code> and replace HTML tag everything works fine. So i added a text like <strong>Add Favorite</strong> and <code>replaceWith</code> <strong>Remove Favorite</strong> but this is how i get my output</p>
  
  <p><a href="https://i.stack.imgur.com/xJPEV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xJPEV.png" alt="enter image description here"></a></p>
  
  <p>as shown in image it is not replacing text but it is adding text, how do i solve this</p>
  
  <pre><code>         jQuery(document).ready(function ($) {
              $('.button').on('click', function (e) {
                  e.preventDefault();
                  var user_id = $(this).attr('user_id'); // Get the parameter user_id from the button
                  var director_id = $(this).attr('director_id'); // Get the parameter director_id from the button
                  var method = $(this).attr('method');  // Get the parameter method from the button
                  if (method === "Like") {
                      $(this).attr('method', 'Unlike'); // Change the div method attribute to Unlike
                      $('#' + director_id).replaceWith('&lt;i class="mi mi_sml text-danger" id="' + director_id + '"&gt; favorite&lt;/i&gt;Remove Favorite'); // Replace the image with the liked button
                  } else {
                      $(this).attr('method', 'Like');
                      $('#' + director_id).replaceWith('&lt;i class="mi mi_sml" id="' + director_id + '"&gt;favorite_border&lt;/i&gt;Add Favorite');
                  }
                  $.ajax({
                      url: 'favs.php', // Call favs.php to update the database
                      type: 'GET',
                      data: {user_id: user_id, director_id: director_id, method: method},
                      cache: false,
                      success: function (data) {
                      }
                  });
              });
          });
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>     function checkFavorite($mbid, $pid, $conn) {
          $stmt = $conn-&gt;prepare("SELECT * FROM favorite WHERE memberID=:mid AND id=:id");
          $stmt-&gt;execute(array(':mid' =&gt; $mbid, ':id' =&gt; $pid));
          $count = $stmt-&gt;rowCount();
  
          if ($count == 0) {
              echo "&lt;div class = 'button' method='Like'  user_id=" . $mbid . " director_id=" . $pid . "&gt;&lt;i class='mi' id=" . $pid . "&gt;favorite_border&lt;/i&gt;Add Favorite&lt;/div&gt;";
          } else {
              echo "&lt;div class = 'button' method='Unlike'  user_id=" . $mbid . " director_id=" . $pid . "&gt;&lt;i class='mi mi_sml text-danger' id=" . $pid . "&gt;favorite&lt;/i&gt;Remove Favorite&lt;/div&gt;";
          }
      }
  </code></pre>