Ask A Question

Notifications

You’re not receiving notifications from this thread.

Render \r\n in javascript.erb

Wouter van den Beld asked in Javascript

HI,

I try to add the value of a text field in a form from a other database field.
I use the following javascript

$("#assignment_conditions").empty();
$("#assignment_conditions").val("<%= @company.conditions%>");

this however fills in

$("#assignment_conditions").empty();
$("#assignment_conditions").val("Op deze opdracht zijn de volgende voorwaarden en condities van toepassing in volgorde van
belangrijkheid:

----

De hierboven vermelde documenten zijn in het verleden eerder verstrekt aan opdrachtnemer en
vormen de basis voor het komen tot een service- en onderhoudsovereenkomst tussen opdrachtgever en
opdrachtnemer");

i need this

$("#assignment_conditions").empty();
$("#assignment_conditions").val("Op deze opdracht zijn de volgende voorwaarden en condities van toepassing in volgorde van\r\nbelangrijkheid:\r\n\r\n----\r\n\r\nDe hierboven vermelde documenten zijn in het verleden eerder verstrekt aan opdrachtnemer en\r\nvormen de basis voor het komen tot een service- en onderhoudsovereenkomst tussen opdrachtgever en\r\nopdrachtnemer.");

when i use @company.conditions.to_json it works because to output is like this

$("#assignment_conditions").empty();
$("#assignment_conditions").val(&quot;Op deze opdracht zijn de volgende voorwaarden en condities van toepassing in volgorde van\r\nbelangrijkheid:\r\n\r\n----\r\n\r\nDe hierboven vermelde documenten zijn in het verleden eerder verstrekt aan opdrachtnemer en\r\nvormen de basis voor het komen tot een service- en onderhoudsovereenkomst tussen opdrachtgever en\r\nopdrachtnemer&quot;);

This adds " so this is not ideally. i tried to_html and gsub but i don't get the right result. Any suggestions?

Reply

So one thing is that \r\n in HTML won't show up as newlines. If you're trying to get those newlines into the HTML on the page, you'll want to replace those with <br/> to force the breaks.

Another thing to note is that normally the newlines are just \n and \r\n is basically just used on Windows for newlines.

You should be able to just do a gsub like this:

$("#assignment_conditions").empty();
$("#assignment_conditions").val("<%= @company.conditions.gsub("\n", "<br/>") %>");

Replace the <br/> with \r\n if that's actually what you need.

Reply

I actually just remembered, Rails provides a helper called simple_format that should do the <br/> replacement for you.

You may also need the <%=j at the beginning of the tag so that any double quotes in your string are escaped so it won't interfer with the Javascript. I put that in there as well just in case.

$("#assignment_conditions").empty();
$("#assignment_conditions").val("<%=j simple_format @company.conditions %>");
Reply

I actually needed the \r\n because when you add .val with jquery on a text field it uses that to put the new lines in the right place.

so when you use simple_format you get the <br/> as text in your field.
Your answer was still great because

$("#assignment_conditions").val("<%=j @company.conditions %>");

did the trick!

Reply

Oh perfect!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.