Ankur Prakash
Ankur Prakash

Reputation: 39

How to check the duplication in Jquery

Hi I need help in Jquery. Actually i create table using Jquery and i don't want to insert those values which is inserted already.

I have one dropdown box in that box i have some ProductName and two textboxes in which i am entering quantity and price. Screenshot

When i click on Add to List button that value is inserted in the table. Screenshot1

Now if i Insert the same item again with different price and quantity again it will be entered. enter image description here

I want when i click on Add to List button i will get an alert message that "Item is already inserted". I have tried a lot for this but I am fail in this.

Let me share you my code. Please help me in this.

$("#addToList").click(function (e) {
    debugger;
    e.preventDefault();

    if ($.trim($("#StockID").val()) == "" || $.trim($("#Quantity").val()) == "" || $.trim($("#Price").val()) == "")
        return;
    var orderid

    var productName = $("#StockID option:selected").text();
    var pid = $("#StockID option:selected").val();
    let price = $("#Price").val();
    let quantity = $("#Quantity").val();

    var tmp1 = {};
    tmp1.productName = $("#StockID option:selected").text();
    tmp1.pid = $("#StockID option:selected").val();
    tmp1.price = $("#Price").val();
    tmp1.quantity = $("#Quantity").val();
    tmp1 = {};

    if (tmp1[productName]) {
        alert('Item is already added !');
    }

    else {
        let detailsTableBody = $("#detailsTable tbody");
        var productItem = `<tr>
                            <td pid=${pid}>
                                ${productName}
                            </td>
                            <td><span data-line_qty="${quantity}" data-itemId="0" href="#" class="qtyItem" >${quantity}</span></td>

                            <td>${price}</td>
                            <td>${(parseFloat(price) * parseInt(quantity))}</td>
                           <td><a data-line_total="${(parseFloat(price) * parseInt(quantity, 10))}" data-itemId="0" href="#" class="deleteItem">Remove</a></td>
                        </tr>`;

        detailsTableBody.append(productItem);
        calc_total();
        clearItem();

    }
});

Upvotes: 0

Views: 37

Answers (1)

Jerry
Jerry

Reputation: 333

$("#addToList").click(function (e) {
    debugger;
    e.preventDefault();

    if ($.trim($("#StockID").val()) == "" || $.trim($("#Quantity").val()) == "" || $.trim($("#Price").val()) == "")
        return;
    var orderid

    var productName = $("#StockID option:selected").text();
    var pid = $("#StockID option:selected").val();
    let price = $("#Price").val();
    let quantity = $("#Quantity").val();

   
    var isAdded = false;
    $("#detailsTable tr").each(function(e) {
        if ($(this).find("> td:first-child").attr('data-pid') == pid) {
            isAdded  = true;
            return false;
        }
    });

    if (isAdded) {
        alert('Item is already added !');
    }

    else {
        let detailsTableBody = $("#detailsTable tbody");
        var productItem = `<tr>
                            <td data-pid="${pid}">
                                ${productName}
                            </td>
                            <td><span data-line_qty="${quantity}" data-itemId="0" href="#" class="qtyItem" >${quantity}</span></td>

                            <td>${price}</td>
                            <td>${(parseFloat(price) * parseInt(quantity))}</td>
                           <td><a data-line_total="${(parseFloat(price) * parseInt(quantity, 10))}" data-itemId="0" href="#" class="deleteItem">Remove</a></td>
                        </tr>`;

        detailsTableBody.append(productItem);
        calc_total();
        clearItem();

    }
});

Upvotes: 1

Related Questions