views:

39

answers:

2

myList contains the following values:

value1
value2
value3

function showArray() {
  var txt = $("#myList").text();
  var textread = txt.split('\n');

  var msg = "";
  for (var i = 0; i < textread .length; i++) {
    msg += i + ": " + textread [i] + "\n";
  }
  alert(msg);
}

my alert gives me the following:

0:value1
value2
value3

It`s not what I wanted and expecting, I was expecting something like:

0: value1
1: value2
2: value3

How can I get the values as expected?

+1  A: 

I tried this with a textarea and it pretty much worked.

The only thing I changed was var txt = $("#myList").text(); to var txt = $("#myList").val();.

Matt Ellen
This works because val() preserves the white space in the textarea, while text() doesn't.
Matt Ellen
A: 

Looks like you have a problem with the character used as line break...

Macmade