How to insert value from select in textarea where there is multiple dropdown and select with same classnames

Viewed 36

I have multiple div with same classnames where I want to insert value from select in the textarea belonging to the div where the select I click on is at. I would like it to be functional with more than 10 div that's why I have just made different classes or ID's for each div

//Insert value form dropdown in textarea
$(document).ready(function(){
    $(".js-example-basic").change(function () {
            $(".textArea").val(this.value);
     }).change();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="selectValue">
  <select name="selectProgram[]" class="js-example-basic">
    <option value="">Choose tour</option>
    <option value="Tour1">Tour 1</option>
    <option value="Tour2">Tour 2</option>
    <option value="Tour3">Tour 3</option>
  </select>
  <textarea name="tourText[]" class="form-control textArea" placeholder="Tour description"></textarea>
</div>
<div class="selectValue">
  <select name="selectProgram[]" class="js-example-basic">
    <option value="">Choose tour</option>
    <option value="Tour1">Tour 1</option>
    <option value="Tour2">Tour 2</option>
    <option value="Tour3">Tour 3</option>
  </select>
  <textarea name="tourText[]" class="form-control textArea" placeholder="Tour description"></textarea>
</div>

Fiddle with code above

1 Answers

This is happen because textarea class is same.

Your are almost there. Use this with next to get desire result on textarea.

//Insert value form dropdown in textarea
$(document).ready(function(){
    $(".js-example-basic").change(function () {
       $(this).next(".textArea").val(this.value);
     }).change();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <select name="selectProgram[]" class="js-example-basic">
    <option value="">Choose tour</option>
    <option value="Tour1">Tour 1</option>
    <option value="Tour2">Tour 2</option>
    <option value="Tour3">Tour 3</option>
  </select>
  <textarea name="tourText[]" class="form-control textArea" placeholder="Tour description"></textarea>
</div>
<div>
  <select name="selectProgram[]" class="js-example-basic">
    <option value="">Choose tour</option>
    <option value="Tour1">Tour 1</option>
    <option value="Tour2">Tour 2</option>
    <option value="Tour3">Tour 3</option>
  </select>
  <textarea name="tourText[]" class="form-control textArea" placeholder="Tour description"></textarea>
</div>

Related