Fill inputs by pasting only one

Asked

Viewed 34 times

1

I have 4 empty inputs and I have 4 paragraphs of text typed in the word. Would there be a way to copy the paragraphs and when paste into the first input it identify line breaks and distribute by filling the others using Jquery? Something similar to creating answers in the Google Classroom form. I can’t identify the breaks.

  • Yes, it is possible. Using the event onpaste, it is possible to capture the data that is "pasted" through Ctrl + V (or Command + V), with this, it is possible to separate the paragraphs and fill the input.

  • Thank you! My problem was in detecting the line break. Thanks!!!

  • and if you have more than 4 paragraphs?

1 answer

0


I think that solves your problem, he takes the line break and mounts a array with 4 positions and add the values in the others </textarea>, paste text and focus to the other element:

$(document).ready(function() {
  $("#tmestre1").on("change", function() {
    const texto = $("#tmestre1").val().split("\n");
    $("#tmestre4").val(texto[3]);
    $("#tmestre3").val(texto[2]);
    $("#tmestre2").val(texto[1]);
    $("#tmestre1").val(texto[0]);    
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea rows="5" cols="33" id="tmestre1"></textarea>
<textarea rows="5" cols="33" id="tmestre2"></textarea>
<textarea rows="5" cols="33" id="tmestre3"></textarea>
<textarea rows="5" cols="33" id="tmestre4"></textarea>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.