1

I have code like this.

<input  type="text" name="1" class = "inp<?=$p?> ">
<input  type="text" name="2" class = "inp<?=$p?> ">
<input  type="text" name="3" class = "inp<?=$p?> ">
<input  type="text" name="4" class = "sum<?=$p?> ">

Here the jquery code

$('.inp0').keyup(function(){       
var sum = 0;
var ave = 0;
$('.inp0').each(function(){
    sum += +$(this).val();
});
 ave  = sum/3;
$('.sum0').val(ave.toFixed(2));
});

and it work properly but just for 1 row of input form.

if i do loop to my jquery code like

for(x=0;x<5;x++)
{*jquery code here*}

and change jquery selector to $('.inp'+x), only 5th row that can work but not for row 1 to 4. can someone help me ? thanks before

6
  • 1
    Just use a common class to all the input fields, no need to have separate classes Commented Apr 7, 2016 at 4:50
  • jsfiddle.net/arunpjohny/1wbsheh6/1 ? Commented Apr 7, 2016 at 4:52
  • do you mean, you have 5 sets of elements like this? Commented Apr 7, 2016 at 4:52
  • if so jsfiddle.net/arunpjohny/1wbsheh6/3? Commented Apr 7, 2016 at 4:55
  • @ArunPJohny i have many sets, not just inp, but inp1, inp1, inp1, sum1 inp2, inp2, inp2, sum2 if i do it manualy it work, but if i do it by loop it doens't work Commented Apr 7, 2016 at 5:01

3 Answers 3

1

Try something like this:

$('[class^="inp"]').keyup(function(){       
var sum = 0;
var ave = 0;
var index = $(this).attr('class').split('p')[1];
$('.inp'+index).each(function(){
    sum += $(this).val();
});
 ave  = sum/3;

$('.sum'+index).val(ave.toFixed(2));
});
Sign up to request clarification or add additional context in comments.

Comments

0

You can group the elemnets by attributes and use that to select the elements like

$('.inp').keyup(function() {
  var sum = 0;
  var ave = 0;
  var grp = $(this).data('grp');
  $('.inp[data-grp="' + grp + '"]').each(function() {
    sum += +$(this).val() || 0;
  });
  ave = sum / 3;
  $('.sum[data-grp="' + grp + '"]').val(ave.toFixed(2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="1" data-grp="0" class="inp">
<input type="text" name="2" data-grp="0" class="inp">
<input type="text" name="3" data-grp="0" class="inp">
<input type="text" name="4" data-grp="0" class="sum">
<br />
<input type="text" name="1" data-grp="1" class="inp">
<input type="text" name="2" data-grp="1" class="inp">
<input type="text" name="3" data-grp="1" class="inp">
<input type="text" name="4" data-grp="1" class="sum">
<br />
<input type="text" name="1" data-grp="2" class="inp">
<input type="text" name="2" data-grp="2" class="inp">
<input type="text" name="3" data-grp="2" class="inp">
<input type="text" name="4" data-grp="2" class="sum">
<br />


Another way

$('.inp').keyup(function() {
  var sum = 0;
  var ave = 0;
  var $tr = $(this).closest('tr');
  $tr.find('.inp').each(function() {
    sum += +$(this).val() || 0;
  });
  ave = sum / 3;
  $tr.find('.sum').val(ave.toFixed(2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>
      <input type="text" name="1" class="inp">
    </td>
    <td>
      <input type="text" name="2" class="inp">
    </td>
    <td>
      <input type="text" name="3" class="inp">
    </td>
    <td>
      <input type="text" name="4" class="sum">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="1" class="inp">
    </td>
    <td>
      <input type="text" name="2" class="inp">
    </td>
    <td>
      <input type="text" name="3" class="inp">
    </td>
    <td>
      <input type="text" name="4" class="sum">
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="1" class="inp">
    </td>
    <td>
      <input type="text" name="2" class="inp">
    </td>
    <td>
      <input type="text" name="3" class="inp">
    </td>
    <td>
      <input type="text" name="4" class="sum">
    </td>
  </tr>
</table>

Comments

0

You have just 1 input classed 'inp0'. Use a common class for all inputs and in the selector.

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.