I am trying to remove an HTML element added to my page after returning from an AJAX call. But jQuery does not seem to know about such dynamically added elements. Any solutions?
Context :
- I am creating a simplified shopping cart.
- When the user clicks the Add button, I add the item to the database. I want to display it on the screen without reloading the page.
- But , since the user can click "Add" several times for the same item in the shopping session, every time he clicks, I want to remove any lines of this item from the DIV shopping cart, and save only the most recent for this item.
This does not work at the moment, so my HTML looks like this after a few additions:
<div id="CartList"> <p id="Item_shirt7363">shirt7363</p> <p id="Item_shirt7363">shirt7363</p> <p id="Item_shirt7363">shirt7363</p> </div>
My jQuery looks like this:
function AddToCartClicked() { item_code = $("#ItemCode").val(); qty = $("#Qty").val(); $.ajax ( { type: "POST", url: "/_res/php/add-item.php", dataType: "text", data: "puid=123&code=" + item_code + "&qty=" + qty, success: function(data) { // PROBLEM: First, remove any existing line with this ID $("#Item_" + item_code).remove(); // Write new line newLine = "<p id='Item_" + item_code + "'>" + item_code + "</p>"; // Add the line in the HTML $("#CartList").prepend(newLine); } } ); return true; }
I searched the forums and did not find a match for my question. Also, I am new to this, so please accept stupidity. Thank you
Hughes98
source share