Ok so I had code on a website which has not changed at all. Two days ago I was using this functionality and it worked, but then today while I was testing things it just stopped working.
For some reason changing from the .children()
selector to .find()
fixed it which I really wasn't expecting.
Here is the code:
var ele = $("div").find("[data-productid='" + response.productId + "']").children(".wishlist-button");
if (response.addedToWishlist) {
$(ele).children(".button-2.active").removeClass("active");
$(ele).children(".remove-from-wishlist-button").addClass("active");
}
I know that .children()
only selects immediate child so here is the html which illustrates that:
<div class="wishlist-button">
<input type="button" value="Remove from wishlist" title="Remove from wishlist" class="button-2 remove-from-wishlist-button remove-from-wishlist-button-6475">
<input type="button" value="Add to wishlist" title="Add to wishlist" class="button-2 add-to-wishlist-button add-to-wishlist-button-6475 active">
</div>
When I changed the code to this using .find()
it worked:
var ele = $("div").find("[data-productid='" + response.productId + "']").find(".wishlist-button");
if (response.addedToWishlist) {
$(ele).find(".button-2.active").removeClass("active");
$(ele).find(".remove-from-wishlist-button").addClass("active");
}
Like I said none of the code had changed, so it's strange for it to just stop working.
I'm using jquery version 1.10.2
I'm wondering if the .children()
function got depreciated, or maybe there is something else going on.
Does anyone know what's going on here? I'm concerned because that .children()
function is used hundreds of times throughout the site.
Thanks