How to read line by line of a text area HTML tag

后端 未结 5 1090
借酒劲吻你
借酒劲吻你 2020-11-29 21:10

I have a text area where each line contains Integer value like follows

      1234
      4321
     123445

I want to check if the user has re

5条回答
  •  再見小時候
    2020-11-29 22:10

    Two options: no JQuery required, or JQuery version

    No JQuery (or anything else required)

    var textArea = document.getElementById('myTextAreaId');
    var lines = textArea.value.split('\n');    // lines is an array of strings
    
    // Loop through all lines
    for (var j = 0; j < lines.length; j++) {
      console.log('Line ' + j + ' is ' + lines[j])
    }
    

    JQuery version

    var lines = $('#myTextAreaId').val().split('\n');   // lines is an array of strings
    
    // Loop through all lines
    for (var j = 0; j < lines.length; j++) {
      console.log('Line ' + j + ' is ' + lines[j])
    }
    

    Side note, if you prefer forEach a sample loop is

    lines.forEach(function(line) {
      console.log('Line is ' + line)
    })
    

提交回复
热议问题