Search code examples
jqueryhtmltargeting

How To Target The class qty inside the add-tocart-btn with my AddtoCart(this) js function


I need to get the input type qty inside the addtocart() pls tell me how to target this Ive tried $(this).find('.qty').val(); but still doesnt work.

<div class="btn btn-sm btn-info add-to-cart-btn"
   onclick="AddtoCart(this)" data-price="'.$a['vkpries'].'" 
   data-arnr="'.$a['arnr'].'" data-mandant="'.$a['mandant'].'" 
   data-name="'.$a['bezeichnung'].'">
   <input type="hidden" ng-value="qty" name="qty" class="qty">    
                                  Add To Cart
</div>

And This is my JS Script

 function AddtoCart(data) {

    var itemname  = data.getAttribute("data-name");
    var itemprice = data.getAttribute("data-price");
    var arnr      = data.getAttribute("data-arnr");
    var mandant   = data.getAttribute("data-mandant");
    //  I need to Get The Quantity inside the button
    console.log(data);


    num += 1;
    swal(itemname , "Has been added to the cart!", "success")
    $('#cart-wrapper').append('<div class="row cart-items" id="item_num'+num+'" onclick="removeItem(this)" data-itemname="'+itemname+'" data-itemprice="'+itemprice+'"><input type="hidden" name="arnr[]" value="'+arnr+'"><input type="hidden" name="price[]" id="price" value="'+itemprice+'"><div class="col-md-12 col-xs-12"><span class="btn btn-sm btn-danger"><i class="fa fa-times"></i></span></div><div class="col-md-12 col-xs-12"><span>'+itemname+'</span></div><div class="col-md-6 col-xs-12"><span>Price = </span></div><div class="col-md-6 col-xs-12"><span> $'+itemprice+'</span></div></div>')
      getTotalPayment();

    // This will calculate all the item prices
}

Solution

  • The value you're looking for is in a child element, the way you have it set up now with the input being the "first()" child, you can just access it with

    $(data).children().first().val()
    

    if it's not first and you add other inputs or elements , you would need to do

    $(data).children([name="qty"]).val()