Barba Barba - 1 year ago 72
Javascript Question

Escape quotes from Javascript in PHP

I have the following code in PHP and it doesn't work:

echo "<script language=\"javascript\">
function rebuild_preview(){

var Title = document.getElementById('title').value;
var LYTtitle = document.getElementById('lyt_title');
if (LYTtitle !== null){
document.getElementById('lyt_title').innerHTML = Title.replace(/\r?\n/g, '<br />');

I think it is because of the quotes in the Title.replace section. Does someone know the correct way to handle it?


Try to use double backslash, one is for php and one for javascript.

 Title.replace(/\\r?\\n/g, '<br />');