1

Morning, I have form with dynamically rows and select option into first column. I would like to set a value into specific element of my row after event select. Actually for each row, all elements change after select.

My form:

 <tbody>
     <tr>
       <td>
         <Select class="form-control form-control-sm" name="item_desc[]" id="item_desc" type="integer" onchange="select_field(this)">
           <option> </option>
            @foreach ($articles as $article)
              <option>{{$article->id}}</option>
            @endforeach
          </Select>
         </td>
         <td class="writefields" name="item_fields[]"></td>
         <td class="writenature"></td>
         <td class="writeuom"></td>
         <td><input class="form-control form-control-sm" type="integer" name="item_quantity[]"></td>
         <td><i class="btn far fa-trash-alt" onclick="remove_row(this)"></i></td>
      </tr>
   </tbody>

My JQuery Code :

function select_field(e)
{
  ...,
    $.ajax({
        type: "POST",
        url: "/achats/selection",
        data: {_token: CSRF_TOKEN, code_item : selection },
        dataType: "JSON",
        success: function (data) {
            $('.writefields').text(data.UoM);
            $('.writeuom').text(data.UoM);
            $('.writenature').text(data.NatureAChat);
        }
    });
}

Thks for your helps !

1 Answer 1

1

If I understand your question correctly then you need something similar to this in order to target the specific row which has changed.

function select_field(e) {
  let test = 'test';

  $(e).parents('tr').find('.writefields').text(test);
  $(e).parents('tr').find('.writeuom').text(test);
  $(e).parents('tr').find('.writenature').text(test);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>
        <select class="form-control form-control-sm" name="item_desc[]" id="item_desc" type="integer" onchange="select_field(this)">
          <option>Select an option</option>
          <option value="1">Item 1</option>
          <option value="2">Item 2</option>
        </select>
      </td>
      <td class="writefields" name="item_fields[]"></td>
      <td class="writenature"></td>
      <td class="writeuom"></td>
      <td><input class="form-control form-control-sm" type="integer" name="item_quantity[]"></td>
      <td><i class="btn far fa-trash-alt" onclick="remove_row(this)"></i></td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what i need... thks for yur help !

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.