I’m hitting some trouble in trying to change the text in a button after a user clicks on the wishlist link. The Ajax call is working successfully right now and the data is updated correctly in the DB, currently the function displays a notification pop up in the browser when a user clicks on wishlist, but I would like to change the "wishlist" part of the button to "Added to wishlist" after a successful submission.
EDIT:
I have added $("#btn span").text("Added to wishlist");
But it’s still not working.
function wish($p_id){
var w_id = $p_id;
var email = $(".wish").data("user");
if(email != 0){
$.ajax({
url:"function.php",
method:"post",
data:{w_id:w_id,email:email},
success: function($wish){
if($wish > 0){
notif({
msg:"Product Already Added to wishlist!!!",
type:"warning",
width:330,
height:40,
timeout:1000,
})
}else{
$("#btn span").text("Added to wishlist");
notif({
msg:"Added to wishlist",
type:"success",
width:330,
height:40,
timeout:1000,
})
}
}
})
}
}
and the HTML part
<a href='$add_wish' id="btn"class='btn btn-default add-to-cart wish' data-user='$s_email' onclick='wish($id)'>
<i class='glyphicon glyphicon-heart'></i> Wishlist
</a>
2
Answers
Identify your button by adding an id and put your text inside a span
You have to change the
innerText
of the span, just add the following code after the product is added :The issue is that in this code:
your selector is missing the target. It tries to find an element with the ID of "btn", which is fine, but then it tries to look for a
<span>
element inside that, which doesn’t exist.It’s trivial to resolve of course by wrapping the text in the expected
<span>
:N.B. Note that writing simply
is undesirable because it will erase the
<i>
containing the icon.Demo (for comparison with above):
Relevant documentation: https://api.jquery.com/category/selectors/