View Full Version : Multiline variables

12-10-2009, 03:29 PM
I am trying to use innerHTML but unsuccessfully on my Wordpress website.
Please see the following code:

var string = "<?php the_content(); ?>";

function textchange() {
document.getElementById('box_content_slideshow').innerHTML = '<div class="box_content">'+string+'</div>';

The problem is that when wordpress puts the content in the variable 'string', it automatically adds carriage returns/line break result in:

var string = "<p>heading</p>
<p>sample text</p>
<p>more text</p>";

Is there any way I can remove the line breaks making the string like:

var string = "<p>heading</p><p>sample text</p><p>more text</p>";

I have tried using:

var string = (<r><![CDATA[

The text string goes here. Since this is a XML CDATA section,
stuff like <> work fine too, even if definitely invalid XML.


But this only works in FF, and not in IE, Opera, or Safari.

Thanks a lot!

12-10-2009, 03:49 PM
innerHTML is not a standard DOM method, thus it will not always insert the elements, their attributes or their events into the DOM tree. Use standard DOM methods instead, in case innerHTML gives errors.

12-10-2009, 04:25 PM
Hi! Thanks for your reply, but I am new to javascript. Can you give any particular example?


rnd me
12-10-2009, 08:31 PM
use php's json_encode:

var string = <?php echo json_encode(the_content()); ?>;