Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138221

Unable to replace text with replaceWith in jquery [duplicate]

$
0
0

I have favorite script which add data to SQL and replace HTML tag everything works fine. So i added a text like Add Favorite and replaceWithRemove Favorite but this is how i get my output

enter image description here

as shown in image it is not replacing text but it is adding text, how do i solve this

         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('<i class="mi mi_sml text-danger" id="' + director_id + '"> favorite</i>Remove Favorite'); // Replace the image with the liked button
                } else {
                    $(this).attr('method', 'Like');
                    $('#' + director_id).replaceWith('<i class="mi mi_sml" id="' + director_id + '">favorite_border</i>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) {
                    }
                });
            });
        });

PHP

     function checkFavorite($mbid, $pid, $conn) {
        $stmt = $conn->prepare("SELECT * FROM favorite WHERE memberID=:mid AND id=:id");
        $stmt->execute(array(':mid' => $mbid, ':id' => $pid));
        $count = $stmt->rowCount();

        if ($count == 0) {
            echo "<div class = 'button' method='Like'  user_id=" . $mbid . " director_id=" . $pid . "><i class='mi' id=" . $pid . ">favorite_border</i>Add Favorite</div>";
        } else {
            echo "<div class = 'button' method='Unlike'  user_id=" . $mbid . " director_id=" . $pid . "><i class='mi mi_sml text-danger' id=" . $pid . ">favorite</i>Remove Favorite</div>";
        }
    }

Viewing all articles
Browse latest Browse all 138221

Trending Articles