Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery html() and new line characters

I'm using jQuery with rails and have the following piece of code

$('#related').html("<%= render :partial => "related_items" %>");

I'm having a problem in the browser where the content of the #related element is only being replaced when there are no line breaks in the partial.

This isn't too much of a big deal, I can place everything on one one line but it makes the code very difficult to read.

Is there a way around having to omit line breaks in the jQuery html() attribute?

like image 828
KJF Avatar asked Oct 26 '25 10:10

KJF


1 Answers

Whats happening here is the code between the <% and %> is being interpreted by Rails first, and the output of that interpretation is being sent to the browser. So what's being sent to the browser is:

$('#related').html("Some text with a
line break in it");

This is not valid javascript because javascript doesn't allow multi-line double-quoted strings. You need to escape the line breaks in the string so that when they are printed to the browser the result is a valid javascript statement.

like image 68
Matt Bridges Avatar answered Oct 29 '25 00:10

Matt Bridges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!