2

I want to add value to attribute. I found this one, but my requirments little different. I want to set value in function onclick="add('id', 5)".

onclick="cart.add('<?php echo $product['product_id']; ?>', here i want to set with jQuery);"

Please also view my code

6
  • 1
    if you are using jQuery, why not bind the event using jQuery? Commented Dec 16, 2015 at 12:37
  • @Pete can you please give jquery bind function reference. I mean how can i use for this task ? Commented Dec 16, 2015 at 12:41
  • @adnankhalid api.jquery.com/on Commented Dec 16, 2015 at 12:43
  • If you give a little bit more context we will be able to provide more help - eg where is that 5 coming from in the add function - edit the question and give your html structure Commented Dec 16, 2015 at 12:44
  • @Pete yes the number is coming from jquery function in input field Commented Dec 16, 2015 at 12:56

1 Answer 1

1

A stronger solution:

Set the data-productid to both the input and the test button:

var cart = { // JUST TO TEST
  add : function(productid, val){
    alert(productid +' '+ val);
  }
}

$(function () { // DOM ready

  $('.qtyplus, .qtyminus').on('click',function(){
    var $qty    = $('.featured-shopping-qty');
    var currVal = Math.abs( parseInt($qty.val(), 10) );
    var isPlus  = $(this).hasClass("qtyplus");
    var calc    = currVal + (isPlus? 1 : -1)
    $qty.val( (!isNaN(calc) && calc>=0) ? calc : 0);
  });
  
  $(document).on("click", ".throwToBasket", function(){
    var productid = $(this).attr("data-productid");
    var val  = parseInt( $("input[data-productid='"+ productid +"']").val() , 10);
    cart.add(productid, val);
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="qtybox">
  <input type="text" name="quantity" value="0" class="qty featured-shopping-qty" data-productid="aaaaa">
</div>
<div class="plusminus">
  <div><input type="button" value="+" class="qtyplus"></div>
  <div><input type="button" value="-" class="qtyminus"></div>
</div>
<a href="javascript:void(0);" class="throwToBasket" data-productid="aaaaa">test</a>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.