0

An attribute of type text needs to be generated within a javascript.

Some records have carriage returns inserted by the user and these need to be passed to javascript in form \n. However when I place

<%= uc.main_text %>

within the javascript, it returns carriage returns

Salties at bottom...

what happens here I am not certain

when \n is needed.

Salties at bottom...\n\nwhat happens here I am not certain

From the console calling the attribute returns a proper string

uc.main_text
[...] \r\n\r\n [...]

How can this be transposed for use by the javascript?

4
  • It's not clear what you're asking. Please provide more information. Commented Jun 13, 2019 at 7:52
  • \r\n\r\n is needed in the rendered javascript, not carriage returns. Commented Jun 13, 2019 at 7:53
  • That doesn't help. Please share what HTML you expect for an example text. Commented Jun 13, 2019 at 7:55
  • javascript escape is required to make this function <%= j (uc.main_text) %> Commented Jun 13, 2019 at 8:07

2 Answers 2

1

You need #escape_javascript:

string = "Salties at bottom...

what \"happens\" here I 'am' not certain"
puts %{var string = "#{escape_javascript(string)}"}

This will print:

var string = "Salties at bottom...\n\nwhat \"happens\" here I \'am\' not certain"
Sign up to request clarification or add additional context in comments.

Comments

0

You can try String.raw(), if I understood your question right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.