Afshin
Afshin

Reputation: 2437

append text to a textarea with line breaks

I want to add some strings to a textarea which are file basenames. Everything is fine, but the only problem is that it mixes all the values and there are not any line breaks:

var file_name = file.file_name;
var base = new String(file_name).substring(file_name.lastIndexOf('/') + 1); 
if(base.lastIndexOf(".") != -1)       
base = base.substring(0, base.lastIndexOf("."));
$('textarea#image_Basename').append(base).split('\n');

These are my file basenames:

5b0cd65710052633dc5dcac406a382c4
212asaddgcvjh622sdsds22113554dfd
5sd5weea55rr6qasfdjkloijhj665s6a

But after storing the data in to the database and retrieving it, the result I get is:

5b0cd65710052633dc5dcac406a382c4212asaddgcvjh622sdsds22113554dfd5sd5weea55rr6qasfdjkloijhj665s6a

Upvotes: 1

Views: 14774

Answers (2)

Afshin
Afshin

Reputation: 2437

My Special thanks to @rink.attendant.6, his second method worked for me :) The answer is:

$('#image_Basename').append(base + '
');

After adding this, I got all the file basenames in separate lines!

Upvotes: 0

rink.attendant.6
rink.attendant.6

Reputation: 46218

To preserve newlines that are coming from a database or whatever, replace the newline characters with the HTML entity for a line feed: 


base = base.replace("\n", '
');
$('#image_Basename').append(base);

If you're trying to append each string with a newline at the end, just concatenate it onto the string:

$('#image_Basename').append(base + '
');

Also, you're using split on the textarea jQuery element, which doesn't make sense as it is an object not a string.

Upvotes: 3

Related Questions