0

I'd like to display certain record from table according combo/drop selection in Laravel. I'm following instructions from this thread but still can not get it to work.

Here is my view code and my jQuery script:

a. combo box

<select type="text" class="form-control" name="cde_mesg" id="cmb_mesg" required>
  <option value="0" disabled="true" selected="true">
    -- PICK MESSAGE-- 
  </option>
  @foreach($message as $messages)
    <option value="{{ $messages->cde_mesg }}">
      {{ $messages->nme_customer }}
    </option>
  @endforeach
</select>

b. text field

<input type="text" name="ad1_contract" id="ad1_contract" class="form-control">

c. jquery script

<script type="text/javascript">
  $(document).ready(function(){
    $("#cmb_mesg").change(function(){
      var x =  $(this).val();
      $("#ad1_contract").val(x);
    });
  });
</script>
Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
mogleng
  • 45
  • 3
  • 11

1 Answers1

0

It doesn't make sense that this is not working as it is presented... unless there is some dynamic element changing after page load perhaps? If so, try changing your jQuery ready function to:

$("#cmb_mesg").on('change', function(){
      var x =  $(this).val();
      $("#ad1_contract").val(x);
});

The benefits of doing the on (delegated) vs change (direct) are well explained in this answer, with further references to other good explanations. You can review further the .on("change", selector, handler) there, as well, to see if that makes a difference.

Watercayman
  • 7,970
  • 10
  • 31
  • 49