1

the code below will auto fill 2 input form based in from another input.. right now its working but only for the first loop..its not working on second and third loop

..i know we have to use something like array but im not sure how to do this..

 <script>
   $(document).ready(function() {
    $('#totalofservices').keyup(function(){
       $("#totalontarget").val($(this).val());
    $("#totalofftarget").val("0");
 });
});
 </script>

 for($x=1; $x<=3; ++$x)
{
    echo "<tr>
      <td>Zone $x</td>
      <td><input style='text-align:center' class='form-control' type='text' size='20' id='totalofservices' name='totalofservices'>
      </td>
      <td><input style='text-align:center' class='form-control' type='text' size='20' id='totalontarget' name='totalontarget'></td>
      <td><input style='text-align:center' class='form-control' type='text' size='20' id='totalofftarget' name='totalofftarget'></td>


    </tr>";
}
1
  • 2
    You're going to have to clarify the question a little better. Commented Jul 3, 2017 at 5:30

2 Answers 2

1

No need a array .Try with class Name instead of id .because the whole document id is unique .Closest() it will target the tr .find() use to find the children

<script>
   $(document).ready(function() {
    $('.totalofservices').keyup(function(){
       $(this).closest('tr').find(".totalontarget").val($(this).val());
    $(this).closest('tr').find(".totalofftarget").val("0");
 });
});
 </script>

 for($x=1; $x<=3; ++$x)
{
    echo "<tr>
      <td>Zone $x</td>
      <td><input style='text-align:center' class='form-control totalofservices' type='text' size='20' id='totalofservices' name='totalofservices'>
      </td>
      <td><input style='text-align:center' class='form-control totalontarget' type='text' size='20' id='totalontarget' name='totalontarget'></td>
      <td><input style='text-align:center' class='form-control totalofftarget' type='text' size='20' id='totalofftarget' name='totalofftarget'></td>


    </tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Firstly, id must be unique and to achieve this use class instead of id Secondly, if you want to use those inputs at server side then make array of input fields like,

$(document).ready(function() {
  $('.totalofservices').keyup(function() {
    var tr = $(this).closest('tr');
    tr.find(".totalontarget").val($(this).val());
    tr.find(".totalofftarget").val("0");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td>Zone 1</td>
    <td><input style='text-align:center' class='form-control totalofservices' type='text' size='20'  name='totalofservices[]'>
    </td>
    <td><input style='text-align:center' class='totalontarget form-control' type='text' size='20'  name='totalontarget[]'></td>
    <td><input style='text-align:center' class='totalofftarget form-control' type='text' size='20'  name='totalofftarget[]'></td>
  </tr>
  <tr>
    <td>Zone 2</td>
    <td><input style='text-align:center' class='totalofservices form-control' type='text' size='20' name='totalofservices[]'>
    </td>
    <td><input style='text-align:center' class='totalontarget form-control' type='text' size='20' name='totalontarget[]'></td>
    <td><input style='text-align:center' class='totalofftarget form-control' type='text' size='20' name='totalofftarget[]'></td>
  </tr>
  <tr>
    <td>Zone 3</td>
    <td><input style='text-align:center' class='totalofservices form-control' type='text' size='20' name='totalofservices[]'>
    </td>
    <td><input style='text-align:center' class='totalontarget form-control' type='text' size='20' name='totalontarget[]'></td>
    <td><input style='text-align:center' class='totalofftarget form-control' type='text' size='20'  name='totalofftarget[]'></td>
  </tr>
</table>

1 Comment

Thank you for the answer

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.