0

I am working on ecommerce website.

My table gets value from session. It looks like : enter image description here

On Checkout I want to get all the values of table.

I have used jquery for this.

Here is my code:

<script>
$('#chhk').on('click',function(e) {
                    e.preventDefault();
                    alert('click');
                var table = $("#tbl tbody");

                 table.find('tr').each(function (i, el) {
                    var $tds = $(this).find('td'),
                        product = $tds.eq(2).text(),
                        price = $tds.eq(3).text(),
                        quantity = $tds.eq(4).attr('value'),
                        total = $tds.eq(5).text();

                     alert('Row ' + (i + 1) + ':\nProduct: ' + product
                          + '\nPrice: ' + price
                           + '\nQuantity: ' + quantity
                          + '\nTotal: ' + total);
                });

            });
</script>

Here '#chhk' is checkout button id

'#tbl' is my table id

I am getting blank value of quantity by this script as it is input field

enter image description here

Any help would be appreciated..

3 Answers 3

1

Try replace this:

                    quantity = $tds.eq(4).attr('value');

with:

                    quantity = $tds.eq(4).val();

As .attr('value') gives the value at the start, while .val() gives current property.

More info: jQuery .val() vs .attr("value")

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

5 Comments

I have tried this before but getting quantity undefined.
kindly check my below answer and let me know if I can use quantity script with .eq syntax
Seems like value is in the input tag, and not in the td tag you get through your .eq syntax. That means that you have to get the input tag out of the td tag. quantity = $tds.eq(4).children("input").val(); might work.
As I have mentioned earlier , it is dynamic table , so it is necessary that input type should be in td
Try quantity = $tds.eq(4).children("input").val(); if you want to keep your .eq syntax.
1

replace this:

 quantity = $tds.eq(4).attr('value'),

with

quantity = $tds.eq(4).find("input").val(),

Comments

0

well I tried to do it separately and am getting expected result.

$('#chhk').on('click',function(e) {
                e.preventDefault();
                alert('click');
            var table = $("#tbl tbody");

             table.find('tr').each(function (i, el) {
                var $tds = $(this).find('td'),
                    product = $tds.eq(2).text(),
                    price = $tds.eq(3).text(),
                    total = $tds.eq(5).text();
                    var qty = $(this).find('td input:first').val();

                 alert('Row ' + (i + 1) + ':\nProduct: ' + product
                      + '\nPrice: ' + price
                      + '\nTotal: ' + total
                      +'\n Qua:' + qty);
            });

        });

Have dealed quantity separately.

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.