0

I am doing a little job in jquery. So my problem is when i click on the anchori get the data and display it in a table using append() .

But again click on the same anchor it duplicate the record. but i do not want to duplicate it just increment 1 value.

Php code is

<?php
    foreach ($product_data as $prod_items) {
?>
<a href="#"class="list-group-item product" data-name="<?=$prod_items['itemname'] ?>" data-price="<?= $prod_items['sellingprice'] ?>">     <?= $prod_items['itemname'] ?> </a>

<?php {
?>

My code is

$('.product').click(function () {

            var name = $(this).attr('data-name');
            var price = $(this).attr('data-price');
            var qty = 2;
            var amount = parseFloat(price)  * parseFloat(qty);

            $(".cat_table").append('<tr><td>' + name + '</td><td>' + price +  '</td><td>' + qty + '</td><td>' + amount + '</td></tr>');


            return false;
        });

Thank you.

3

3 Answers 3

1

just unbind it's click event after it execute inorder for it to not click again and avoid duplicate record

DEMO

you just add

$(this).unbind('click');

after you append data into the table

you should declare qty outside the function so that it's value will not reset everytime your event click execute

var qty = 1;
$('.product').click(function () {

    var name = $(this).attr('data-name');
    var price = $(this).attr('data-price');
    var amount = parseFloat(price)  * parseFloat(qty);

    $(".cat_table").append('<tr><td>' + name + '</td><td>' + price +  '</td><td>' + qty + '</td><td>' + amount + '</td></tr>');

    $(this).unbind('click');

    alert(qty);

    qty++; // to increment you qty every time you click

    return false;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Great. But i want to increment a value of qty on each click. your answer solve the first part of my problem but not the second :-(
0

try this,

$('.product').click(function () {    
              var name = $(this).attr('data-name');
              var price = $(this).attr('data-price');
              var qty = 2;
              var amount = parseFloat(price)  * parseFloat(qty);
              $(".cat_table").empty();
              $(".cat_table").append('<tr><td>' + name + '</td><td>' + price +  '</td><td>' + qty + '</td><td>' + amount + '</td></tr>');
              return false;
        });

DEMO

2 Comments

I am working in a loop. So there are also other values which can be add. So i do not want to empty the table first and add new value. what i want is it should append new values but not the old one which already added. if the value exists it should increment it not duplicate it.
<?php foreach ($product_data as $prod_items) { ?> <a href="#"class="list-group-item product" data-name="<?= $prod_items['itemname'] ?>" data-price="<?= $prod_items['sellingprice'] ?>"><?= $prod_items['itemname'] ?>
0

You need to declare your variables outside the click function, so that they are treated as global and does not loose their value until page is reloaded. Hope this will help you.

<script>

            var qty = '';
            var amount = parseFloat(price)  * parseFloat(qty);
$('.product').click(function () {

            var name = $(this).attr('data-name');
            var price = $(this).attr('data-price');
    qty = qty + 2;
        amount = amount + parseFloat(price)  * parseFloat(qty);

            $(".cat_table").append('<tr><td>' + name + '</td><td>' + price +  '</td><td>' + qty + '</td><td>' + amount + '</td></tr>');


            return false;
        });
</script>

8 Comments

this is like comment and not the answer.
@AshishRanade — "A good developer understand a good comment and its value", we are not that good developers Sir.
$('.product') appears <tr> to me, How will you get the data-* attributes without knowing the this context ? How will having them global will help ?
Yes, I came to know that now :)
@AshishRanade - i don't think the description provided by OP is that much sufficient. So by providing answer will just eat up OP's more time.
|

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.