user3239674
user3239674

Reputation: 67

Button class pass through Ajax not working

I have some problem. I am using Laravel. I pass html string from controller using Ajax. Example:

 $html .= '<span class="book-wishlist-btn addwish" id="addwish'.$book->id.'" data-placement="left" title="'.$title.'" style="'.$style.'" book_id="'.$book->id.'">';
                $html .= '<i data-feather="heart">';
                $html .= '</i>';
                $html .= '</span>';

At my blade, I have 2 Ajax which is to retrieve data I pass from controller and to run class addwish. Span class id (addwish) will trigger based on jquery click function.

Ajax to retrieve $html string:

$.ajax({
                method:"POST",  
                dataType: "json",
                url: "{{url('books')}}",
                data: {html:html},
                success: function(data){
                    feather.replace();
                }
            });

Ajax to trigger addwish class click:

 $(".addwish").click(function(){

            $.ajax({  
                url : "{{url('addwishlist')}}",
                //my codes goes here..
            });
        });

My problem is the addwish class not detect. Anyone can help give an idea? Thank you

Upvotes: 1

Views: 330

Answers (1)

Amin Eshtiaghi
Amin Eshtiaghi

Reputation: 196

Use this code instead of your previous click code:

$(".addwish").on("click", function() {
    $.ajax({  
        url : "{{url('addwishlist')}}",
        //my codes goes here..
    });
});

reference to : Click not working after ajax content has loaded

Upvotes: 1

Related Questions