coffeeNcode
coffeeNcode

Reputation: 337

How to return text from window.prompt() that retains line breaks?

I am creating an option on my forum to include external text via a button that will include it formatted into a textarea. I am getting the text via copy and paste into a prompt but the string is returned as one big wall of text, the line breaks are gone. Is there anyway to preserve them or is this an issue with the actual copying of the text?

This is what I have so far:

function createExText(textbox) {
    var extext = window.prompt('Enter external text:','');
    var formattedextext = '[extext]'+ extext +'[/extext]';
    insertAtCaret(textbox, formattedextext);
}

Upvotes: 2

Views: 1311

Answers (1)

Guilherme Sehn
Guilherme Sehn

Reputation: 6787

I do not know any browser implementation of prompt that allows multi-line content. You could create a custom modal implementation with a textarea inside to simulate it.

As @RGraham commented, some browsers like Google Chrome actually preserve the line break characters if you paste an external text into it, however it is shown in only one line inside the dialog. I'd not stick with this solution as it is not user friendly and doesn't seem to work cross browser.

Upvotes: 4

Related Questions