New delimiter for textarea in html

I am trying to give the user the opportunity to create a custom list for my application by inserting his selection in the text area field, each line is considered as one of the options, using \ n (enter) as a separator, I need to allow the user to enter one choice in several lines, any may I help?

<textarea></textarea>

enter the user: aa bb cc with enter in textarea, and then I process them to split them into an input separator, so I have three options in the list: 1-aa, 2-bb and 3-cc, but I need do aa, bb first option and second variant cc, i need a new delimiter, i need more ideas?

+3
source share
3 answers

, Enter:

JavaScript

<script type="text/javascript">
  var LastKeyCode=-1;

  function updateOptions(e) {
    if (e.keyCode) {
      var KeyCode=e.keyCode;
    } else {
      var KeyCode=e.charCode;
    }

    if (KeyCode==13 && LastKeyCode==13) { // Enter?
      var RowsDataAry=document.getElementById('inputfield').value.replace(/\r\n/g,"\n").split(/\n{2,}/g);  // Fix IE CRs and split the textarea input

      document.getElementById('choices').options.length=0;  // Empty select

      for (RowID in RowsDataAry) {
        var TextVal=RowsDataAry[RowID].replace(/\n/g,", ").replace(/, $/,"");   // Set commas and remove the last comma

        document.getElementById('choices').options.length++;
        document.getElementById('choices').options[RowID].value=RowID;  // Add option value
        document.getElementById('choices').options[RowID].text=TextVal;
;  // Add option text
      }
    }

    LastKeyCode=KeyCode;
  }
</script>

HTML

<select id="choices" size="20"></select>

<textarea id="inputfield" onkeypress="updateOptions(event)" cols="40" rows="20"></textarea>

Firefox 3.6.3, Opera 10.53, IE 8 Iron 5.0.380.

0

, , , .

, - , .

.

:

Item One

Item Two line 1
Item Two line 2

Item Three
+2

, . , ( ) . ( ), .

Remember to tell the user what to use as the separator.

+1
source

Source: https://habr.com/ru/post/1750902/


All Articles