Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery each line in textarea

Tags:

jquery

html

<textarea id="gps" name="gps"></textarea>
<button>Click</button>

jquery

$('button').click(function(){
    var arrayOfLines = $('#gps').val().split('\n');
    $.each(arrayOfLines, function(index, item) {
        $this = $(this);
        console.log($this);         
    });
});

I am trying to output each line individually so that I can use them later on but at the moment the above seems to split each line and then place each letter as an object

JSBin

like image 984
ngplayground Avatar asked Nov 08 '13 11:11

ngplayground


2 Answers

You are placing a string into a jQuery object. Just use the item instead:

$('button').click(() => {
  var arrayOfLines = $('#gps').val().split('\n');
  arrayOfLines.forEach(item => console.log(item));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<textarea id="gps" name="gps">28.73514, -147.42323
4.09974, 66.93197
49.11390, 48.85446</textarea>
<button>Click</button>
like image 171
Rory McCrossan Avatar answered Nov 10 '22 11:11

Rory McCrossan


Inside .each loop line id 'item' object, not 'this'.

<textarea id="gps" name="gps"></textarea>
  <button id="btn">Click</button>
  $('#btn').click(function(){
      var arrayOfLines = $('#gps').val().split('\n');
      $.each(arrayOfLines, function(index, item) {
          console.log('here is line:', item);         
      });
  });
like image 22
konclave Avatar answered Nov 10 '22 11:11

konclave