Okay, so here's the dilemma:
Is there a way to use JavaScript to determine if a DIV has a specific classname? For instance, consider this sample HTML
<div class="cart"></div>
. This DIV would act as the parent, and JavaScript would dynamically create child divs underneath it upon clicking. However, if a child div is already present and you click on the item, it should be removed. For example, imagine a button that turns yellow when clicked, adding an item to the cart and changing the button's class to "selected." Clicking the button again should remove the selected class and eliminate the item from the cart. I have code for adding items to the cart, but I'm struggling with removing them. Any suggestions?
JavaScript
$(".item-card").mousedown(function() {
$(this).toggleClass("selected-item");
var itemName = $(this).find("img").attr("title");
$("#itemcart").append($("<div id="+itemName+">"+itemName+"</div>"));
});
Currently, clicking repeatedly adds the item to the cart multiple times. I've attempted to create a function that checks if the item is already in the cart, removes it if present, and adds it if not. However, I haven't been successful in implementing this. I believe addressing this issue would resolve my problem.