Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Attr (ID) of Dynamically Added <li> Element with jQuery
    primarykey
    data
    text
    <p>I'm having issues getting ID of a dynamically added list element. Which is being added with the following:</p> <pre><code>$(".detail-view button").on("click", function(){ var noItems = $("ul#cart-items li").eq(0).attr("id"); var detailID = $(this).attr('id'); var orderTitle = $("#detail-"+ detailID + " span.order-title").text(); var insertTitle = $("&lt;li&gt;&lt;a href=\"#\" class=\"remove-item\"&gt;&lt;img src=\"/assets/images/global/intra_remove_item_btn.png\" /&gt;&lt;/a&gt;"+ orderTitle +"&lt;/li&gt;").attr("id", detailID); if(noItems == "emptycart") { $("ul#cart-items li#emptycart").replaceWith(insertTitle); $("#lp-orderform li:even").addClass("highlight"); } else { $(insertTitle).insertAfter("ul#cart-items li:last"); $("#lp-orderform li:even").addClass("highlight"); } }); </code></pre> <p>Which updates my original HTML from the following...</p> <pre><code>&lt;ul id="cart-items"&gt; &lt;li id="emptycart"&gt;You have no items in your cart.&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>And updates to...</p> <pre><code>&lt;ul id="cart-items"&gt; &lt;li id="6955" class="highlight"&gt;&lt;a href="#" class="remove-item"&gt;&lt;img src="/assets/images/global/intra_remove_item_btn.png"&gt;&lt;/a&gt;Chicken with Garlic Sauce&lt;/li&gt; &lt;li id="6966"&gt;&lt;a href="#" class="remove-item"&gt;&lt;img src="/assets/images/global/intra_remove_item_btn.png"&gt;&lt;/a&gt;Hunan Shrimp with Black Bean Sauce&lt;/li&gt; &lt;li id="6965" class="highlight"&gt;&lt;a href="#" class="remove-item"&gt;&lt;img src="/assets/images/global/intra_remove_item_btn.png"&gt;&lt;/a&gt;Hunan Pork with Black Bean Sauce&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I'm trying to get the ID of the list item when a.remove-item is clicked to remove that list element.</p> <pre><code>$("ul#cart-items").live("click", "li a.replace-item", function(){ var removeItemID = $("li a.replace-item").attr("id"); alert(removeItemID); }); </code></pre> <p>But I only seem to get a value of "undefined". Any input would be greatly appreciated. Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload