0

i have 1 fields of input type...and if i changed the value,it will multiplied the values on the loop..here is the example...

<input type='number' id='values' name='values' onkeyup='multiplied()'>
<?php 
   foreach($data as $value)
   {
     $no = 1;
     echo "<input type='number' class='form-control value' id='value' name='value' readonly value=".$value['value'].">";
     echo "<input type='number' class='form-control' id='total' name='total' readonly >";
     $no++;
   }

?>

function multiplied()
{
  var values = $('#values').val();
  var value = $('.value').val();
  total = values * value;
  $('#total').val(total)
}

how do i multiplied values dynamically when i change the values of id='values'
Thank you,any helps will be appriciated

1
  • 1
    It would be useful and easier to help if you'd posted the generated HTML as opposed to PHP! Commented Oct 7, 2017 at 12:02

2 Answers 2

1

You can use jquery on function.

$(document).on('change', '#values', function() {
        var values = $("#values").val();            
        $(".value").each(function(index, element){
            values *= element.value;                 
            $(this).next('input#total').val(values);
        });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Wrapping this back into a jQuery constructor just to get a value? Use this.value instead.
@RokoC.Buljan I've edited my code, is that you meant ? I could shorten my code by one line.
0

Dont use same id multiple times. Use class instead. Also you have to loop through element when you have to change value

change

echo "<input type='number' class='form-control' id='total' name='total' readonly >";

to

echo "<input type='number' class='form-control total' name='total' readonly >";

And in script

<script>
function multiplied()
{
  var values = $('#values').val();
  $( ".value" ).each(function() {
      var value = $(this).val();
      total = values * value;
      $(this).next('input').val(total);
});

}
</script>

Also make sure you total input field is exactly next after value input field

OR you can also use nextAll with class name if total input field is not exactly next after value input field

<script>
function multiplied()
{
  var values = $('#values').val();
  $( ".value" ).each(function() {
      var value = $(this).val();
      total = values * value;
      $(this).nextAll('.total').val(total);
});

}
</script>

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.