This is the code
html
<a href="#" data-postid="{{ $post->id }}" data-userid="{{ Auth::user()->id }}" class="like-post">
@if(Auth::user()->likes()->where(['user_id' => Auth::user()->id, 'post_or_comment_id' => $post->id,'isPost' => 1])->first())
<span class="glyphicon glyphicon-star"></span>like
@else
<span class="glyphicon glyphicon-star-empty"></span>like
@endif
</a>|
jquery
$('.like-post').on('click',function(event){
event.preventDefault();
var userId = event.target.dataset['userid'];
var postId = event.target.dataset['postid'];
$.ajax({
method:'POST',
url:likeUrl,
data:{userId:userId, p_or_c:postId, isPost:1, _token:token},
success:function(msg){
if(msg['like'] == 0)
$(event.target).html('<span class="glyphicon glyphicon-star-empty"></span>like');
else
$(event.target).html('<span class="glyphicon glyphicon-star"></span>like');
console.log(msg['like']+'yes');
}
})
});
every thing works fine but when I change :
<span class="glyphicon glyphicon-star"></span>like
to (in both html and jquery files ):
<span class="glyphicon glyphicon-star"></span><b>like</b>
or
<b><span class="glyphicon glyphicon-star"></span>like</b>
this happens before adding b tag ... everything fine screen shot 1 after adding b tag screen shot 2
I get this error HTTP/1.1 500 Internal Server Error and I tried many things but it seems that jquery doesn't accept two tags in html() cause I tried to change b tag to span tag and the same error appears and thanks in advance
Try:
$(event.target).append('<span class="glyphicon glyphicon-star"></span><b>like</b>');
I create a jsFiddle for you: jsFiddle
Try this snippet, similar to your example.
When you append an HTML, there is no matter how many tags you include in that. The HTML should be proper, That's it.
$('.like-post').click(function(){
$(event.target).html("<span class='glyphicon glyphicon-star'></span><b>like</b>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" data-postid="105" data-userid="505" class="like-post">
Click
</a>
</div>