1

I am having trouble getting some data from a textbox using javascript. What I am trying to do is take the value from a textbox and put it in an alert and copy it. Here is the code that I currently have:

var copyString = "Date: <%= TXT_Details_DateReq.Text %>;
window.prompt('Press CTRL+C, then ENTER\n\nNOTE: SAVE ANY CHANGES BEFORE COPYING TEXT!', copyString); return false;

So this code works perfectly fine if the text in the textbox is just one line. But if the text in the textbox has multiple lines such as:

"This is one line
here is a second line"

The code will throw the error Uncaught Syntax Error: Unexpected Token ILLEGAL. From what I have researched this throws when there is an illegal character, so I believe it is the CRLF character from the textbox.

Any idea how to fix this?

Thanks.

Chase Ernst
  • 1,147
  • 1
  • 21
  • 53

1 Answers1

-1

New line characters might be preceded by return characters ('\r').

Swap '\n' with '\r\n' and you should be good to go.

Or even better: just handle all cases of the new line character instead of checking which case then applying it. This replace the newline:

htmlstring = stringContainingNewLines.replace(/(\r\n|\n|\r)/gm, "<br>");

Resource

hope it helps.

Community
  • 1
  • 1
U r s u s
  • 6,680
  • 12
  • 50
  • 88
  • This is for Javascript. The text with the newline is from a server control, so the value has to be replaced on the server and not on the client. Furthermore a `
    ` tag won't work at all in a prompt.
    – Andreas Feb 02 '15 at 16:34
  • The main tag on the OP _is_ Javascript. – U r s u s Feb 02 '15 at 16:37
  • And how you think you can fix the `Uncaught Syntax Error: Unexpected Token ILLEGAL` error in the Javascript part with Javascript?! – Andreas Feb 02 '15 at 16:38