Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Location
    London
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Multiline variables

    Hi!
    I am trying to use innerHTML but unsuccessfully on my Wordpress website.
    Please see the following code:
    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:
    Code:
    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:
    Code:
    var string = "<p>heading</p><p>sample text</p><p>more text</p>";
    I have tried using:
    Code:
    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.
    
    ]]></r>).toString();
    But this only works in FF, and not in IE, Opera, or Safari.

    Thanks a lot!
    Cheers!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Location
    London
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi! Thanks for your reply, but I am new to javascript. Can you give any particular example?

    Thanks!

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    use php's json_encode:
    Code:
    var string = <?php echo json_encode(the_content()); ?>;
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •