Ya Basha
Ya Basha

Reputation: 1952

jQuery read values and do calculations

I have html table with 3 rows each row contains two <td> one with text input field with name="freq" and the second <td> is empty.

I want to read all the values of input fields and do calculation function on them then set the results for every field in the empty <td>

I want to do that in jQuery,

Thanks for any help.

Upvotes: 4

Views: 149

Answers (3)

Vikas Naranje
Vikas Naranje

Reputation: 2392

try this

$('#table :input').change(function(){
  var val = $(this).val();
  val = eval(val) * 5; //your calculation
  $(this).parents('td:first').next().html(val);
});

Upvotes: 0

hsz
hsz

Reputation: 152206

Try with:

$('#yourTable td[name="freq"] input').change(function(){
  var val = parseInt($(this).val());
  val = val * 3 + 4; //your calculation
  $(this).parent().next().text(val);
});

Upvotes: 2

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195982

Something like this..

$('input[name="freq"]').each(function(){
  var value = this.value; // extract the value from the input element
  var newValue = process(value); // process performs the calculations..

  $(this).closest('td').next().html(newValue); // find the following <td> element and set its contents to the result of the calculations..
});

Demo at http://jsfiddle.net/gaby/bWFxQ/

Upvotes: 4

Related Questions