0

I need your help please. I have a scenario where I appending value from the dropdown to the text area. I'm facing an issue where I cannot appending the same value from the dropdown.

For example I write at text area text hello then insert "User name" then try to insert "User name" again and it dose not inserted.. only another option I can insert. But if I insert another option then I can insert "User name" again.

<select name="email_admin" class="form-control variables_select">
    <option value="">--Select--</option>       
    <option value="user_name">User name</option>
   <option value="ticket_number">Ticket #</option>
   <option value="ticket_subject">Ticket subject</option>            
</select>

$('.variables_select').change(function(){
  var current_select_list = $(this).attr('id');
  var id = $(this).closest('.panel-body').find('textarea').attr('id');
  var cursorPosStart = $('#'+ id).prop('selectionStart');
  var cursorPosEnd = $('#'+ id).prop('selectionEnd');
  var v = $('#' + id).val();
  var textBefore = v.substring(0,  cursorPosStart);
  var textAfter  = v.substring(cursorPosEnd, v.length);
  if ($('#' + current_select_list + ' option:selected').val().length > 0) {
      $('#' + id).val(textBefore + '{{' + $('#' + current_select_list + ' option:selected').val() + '}}' + textAfter);
  }
  $('#'+ id).prop('selectionStart', cursorPosStart);
  $('#'+ id).prop('selectionEnd', cursorPosStart + '{{' + $('#' + current_select_list + ' option:selected').val() + '}}'.length);
});

html.

<div class="panel-body">
    <div class="input-group pull-right margin-bottom-1">
      <?php
      echo Form::select('templates_variables_logs_staff', $templatesVariables, '', array('class' => 'form-control variables_select'));
      ?>
    </div>
    <?php
    echo Form::label('logs_staff', __('Content', 'notifications'), array('class' => 'control-label'));
    echo Form::textarea('logs_staff', $logs_staff, array('class' => 'form-control'));
    ?>
</div>
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42

2 Answers2

0

That happens because you are using change event when you select username and again you select username you don't change the select box value so it doesn't call your script.

If you need to add same value, you need to use click event instead of change event like this.

Note that click event may call your code multiple so you need to use some extra data to know this click is OK or not. To do that I've used extra attribute (say clicks) and check to see if has value is 1 or not. Also I need to reset the attribute value in focusout event:

 $('.variables_select').focusout(function () {
     $(this).data('clicks', 0);
 });

Here is working sample:

 $('.variables_select').click(function () {
     if ($(this).data('clicks') == 1) {
         $(this).data('clicks', 0);

         var current_select_list = $(this).attr('id');
         var id = $('.panel-body').find('textarea').attr('id');
         var cursorPosStart = $('#' + id).prop('selectionStart');
         var cursorPosEnd = $('#' + id).prop('selectionEnd');
         var v = $('#' + id).val();
         var textBefore = v.substring(0, cursorPosStart);
         var textAfter = v.substring(cursorPosEnd, v.length);
         if ($('#' + current_select_list + ' option:selected').val().length > 0) {
             $('#' + id).val(textBefore + '{{' + $('#' + current_select_list + ' option:selected').val() + '}}' + textAfter);
         }
         $('#' + id).prop('selectionStart', cursorPosStart);
         $('#' + id).prop('selectionEnd', cursorPosStart + '{{' + $('#' + current_select_list + ' option:selected').val() + '}}'.length);
     }
     else
         $(this).data('clicks', 1);
     });

 $('.variables_select').focusout(function () {
     $(this).data('clicks', 0);
 });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"
            integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
            crossorigin="anonymous"></script>

<select name="email_admin" class="form-control variables_select" id="myselect">
        <option value="">--Select--</option>
        <option value="user_name">User name</option>
        <option value="ticket_number">Ticket #</option>
        <option value="ticket_subject">Ticket subject</option>
    </select>

    <div class="panel-body">
        <div class="input-group pull-right margin-bottom-1">
            <textarea id="textarea"></textarea>
        </div>
    </div>
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
-1

Here is a somewhat simplified version that hopefully helps you clean up your code and get you to where you need to be:

$('.variables_select').change(function() {
  var selectedOptionValue = $(this).val();
  var textAreaElement = $("#ta")[0];
  var textAreaValue = $(textAreaElement).val();
  var newTextAreaValue = textAreaValue + "\n" + selectedOptionValue;
  $(textAreaElement).val(newTextAreaValue);
  $('.variables_select').val("");// This line resets the select so you can choose the same option again.
});
#ta {
  height: 100px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="email_admin" class="form-control variables_select">
  <option value="">--Select--</option>
  <option value="user_name">User name</option>
  <option value="ticket_number">Ticket #</option>
  <option value="ticket_subject">Ticket subject</option>
</select>

<textarea id="ta"></textarea>
Rob Moll
  • 3,345
  • 2
  • 9
  • 15