locked
Change the button caption if array containing value RRS feed

  • Question

  • User-257070954 posted

    In My Shopping application, if one product added to his cart i want to change product list button "Add to cart" caption to "Numerber Qty Added + In basket".
    As you know each and every product listed have the "Add to cart" Button
    Below giving my code

    var CartArray = [];
    var CartArrayData = {};

    CartArrayData.ID = 1
    CartArrayData.ProductName = "Product1"
    CartArrayData.Qty = 5

    CartArray.push(CartArrayData);

    As per my above code, I added Product1 added to CartArray
    Now i want to change product1 "Add to cart" Button caption to "5 In basket". So how can achieve this using jquery

    Wednesday, June 17, 2020 12:45 PM

All replies

  • User475983607 posted

    You are asking this question as if the community can see your design and code.   First, is this Web Forms, MVC, Core?  Can you share the initial page load and jQuery/JavaScript application?  How are items added to the shopping cart?  Are you calling a service?

    Wednesday, June 17, 2020 1:03 PM
  • User-257070954 posted

    Hi mgebhard, 

      As per your requesting i am giving my code


    <body>
        <div style="width:1536px; height:864px">
            <input type="text" id="txtSearch" />
            <input type="button" id="btnSearch" value="Search" />
            <input type="button" id="btnViewCart" value="ViewCart" />
            <input type="button" id="btnAddOrder" value="Add Order" />
            <div id="divListProduct" style="width:900px; height:850px;"></div>
            <div id="divCart" style="width:300px; height:500px ; background-color:yellow;float:right; margin-top:50px; margin-right:250px"></div>


        </div>
    </body>


    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script>
        var CartArray = [];
        var CartCout = 0
        var TotalAmount=0


        $(document).ready(function () {
            GetAllProduct(); // Page load All products loading
        });
      
       
        function GetAllProduct() {
            var search = $("#txtSearch").val()
            $.ajax({
                type: "GET",
                url: "https://localhost:44360/api/Product/GetSearchProduct?Products=" + $("#txtSearch").val() ,
                dataType: "json",
                success: function (result, status, xhr) {
                    ListProduct(result)
                },
                error: function (xhr, status, error) {
                    alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
                }
            });
        }


        $("#btnSearch").click(function () {
            GetAllProduct() // Loading product based on product name entered in search  textbox
        });
        $("#btnViewCart").click(function () {
            ViewCart() // To show all product added to his cart
        });
        //$("#btnAddOrder").click(function () {
        //    alert("Hi")
        //});


        function ListProduct(Product) {  //Ui creation of product list once we received  data from API
            var obj = jQuery.parseJSON(Product.items);
            var ProductHtml=''
            $.each(obj, function (key, value) {


                ProductHtml += "<div id=divParenList" + key + ";style=width:300px;height:200px;background-color:aqua;1px solid red>"
                    + "<span id = spnProdListProdName" + key + ">" + value.ProductName + "</span>&nbsp;&nbsp;&nbsp; "
                    + "<span id = spnProdListBarnd" + key + ">" + value.Brand + "</span>&nbsp;&nbsp;&nbsp;"
                    + "<span id = spnProdListPrice" + key + ">" + value.Price + "</span> <br>"
                    + "<img src=data:image/jpeg;base64," + value.Image2 + " width=200 height=80 /><br>"
                    + "<span id = spnProdListQty" + key + " > <input type=text id=txtProdListQty" + key + " class=allownonlyumeric /></span> <br>"
                    + "<input type=button id=btnAddCart-" + key + " value = Add onClick = AddToCart(this.id) />"
                    + "<input type=hidden id=hdnProdListProductId" + key + " value = " + value.Id+" />"
                    +"</div > ";
            }); 
            $("#divListProduct").html(ProductHtml)
        }


        


        function AddToCart(id) { //Adding to  CartArray When we  click Add to cart button


            var res = id.split('-');
            var ProdId = res[1]
            var CartArrayData = {};
            CartArrayData.Id = $("#hdnProdListProductId" + ProdId).val()
            CartArrayData.ProductName = $("#spnProdListProdName" + ProdId).html()
            CartArrayData.BrandName = $("#spnProdListBarnd" + ProdId).html()
            CartArrayData.ProdPrice = $("#spnProdListPrice" + ProdId).html()
            CartArrayData.Qty = $("#txtProdListQty" + ProdId).val()
            CartArrayData.TotalPrice = parseInt($("#spnProdListPrice" + ProdId).html()) * parseInt($("#txtProdListQty" + ProdId).val())
            CartArray.push(CartArrayData);
        }


        function ViewCart() { //Ui creation of Basket(ViewCart)  When we click on ViewCart Button
            $.each(CartArray, function (key, value) {
                TotalAmount += value.TotalPrice
                $("#divCart").append("<div id=divCartParentList;style=width:300px;height:200px;background-color:orange>"
                    + "<span id=spnCartProdName" + key + ">" + value.ProductName + "</span>"
                    + "&nbsp;&nbsp;&nbsp;<span id=spnCartProdBrand" + key + ">" + value.BrandName + "</span>"
                    + "&nbsp;&nbsp;&nbsp;<span id=spnCartProdPrice" + key + ">" + value.ProdPrice + "</span>"
                    + "&nbsp;&nbsp;&nbsp;<span id=spnCartProdQty" + key + ">" + value.Qty + "</span>"
                    + "&nbsp;&nbsp;&nbsp;<input type=hidden id=hdnCartProdId" + key + " value=" + value.Id + "/>"
                    + "&nbsp;&nbsp;&nbsp;<span id=spnCartItemPTotalrice" + key + ">" + parseInt(value.Qty) * parseInt(value.ProdPrice) + "</span>&nbsp;&nbsp;&nbsp;"
                    + TotalAmount + " </div >"
                )
            });
        }
        
    </script>

    Wednesday, June 17, 2020 1:42 PM
  • User475983607 posted

    The ListProducts() function should check if the product key is in the cart.  If the key is found then render the caption.  Are you asking how to find an item in a JavaScript array?

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

    Wednesday, June 17, 2020 2:21 PM
  • User288213138 posted

    Hi binustrat,

    In My Shopping application, if one product added to his cart i want to change product list button "Add to cart" caption to "Numerber Qty Added + In basket".
    As you know each and every product listed have the "Add to cart" Button
    Below giving my code

    var CartArray = [];
    var CartArrayData = {};

    CartArrayData.ID = 1
    CartArrayData.ProductName = "Product1"
    CartArrayData.Qty = 5

    CartArray.push(CartArrayData);

    Based on your description and code, I cannot understand your requirement.

    Where is the code for the product list button? what is Numerber Qty Added + In basket?  and what is "Add to cart" Button?

    So please post more information about your requirement.

    Best regards,

    sam

    Thursday, June 18, 2020 2:32 AM