Kiran
Kiran

Reputation: 8538

How to get the input field value through Javascript?

I have a dynamic table which has the input field in each row. I would like to get the value entered by the user through Javascript. I am able to get the HTML code for the input field 'quantity' but not the value.

Here is the part of the javascript code :

var table = document.getElementById("mactable");
var row_count = table.getElementsByTagName("tr").length;
var grand_total = document.getElementById("grand_total");
var grand_price = 0.00;
for (var i = 1, row; row = table.rows[i]; i++) 
{
    var price = row.cells[4].innerHTML;
    var quantity = row.cells[2].innerHTML;
    alert(quantity);
    if(isNumber(price))
    {
        var num = parseFloat(price);
        grand_price = grand_price + num;
    }
}

Any idea how can it be done ?

Upvotes: 1

Views: 85

Answers (1)

dfsq
dfsq

Reputation: 193291

Instead of reading table cell innerHTML you can find inner input element and then read its value property:

var quantity = Number(row.cells[2].querySelector('input').value);

Here row.cells[2] is a td element. Using querySelector('input') you can find the first children input.

Also note, that since the value of the field is a string, you might want to cast it to number, for example with Number function.

Upvotes: 2

Related Questions