0

Possible Duplicate:
How do I break a string across more than one line of code in JavaScript?

Hi i am new to javascript and i struggling to get this to work.

Basically everything works fine, but as soon as there are any spaces within it, it breaks.

Its pulling from the database a text string, which is fine until there is a space like as follows:

var getHtml = '<div class="counterwrap"><div class="countertitle"><h1><?php echo $ublunderconstructioninfo->title ; ?></h1></div><div class="counter"><div class="counternumbers"><div id="counterdays"></div><p>Days</p></div><div class="counternumbers"><div id="counterhours"></div><p>Hours</p></div><div class="counternumbers"><div id="counterminutes"></div><p>Minutes</p></div><div class="counternumberslast"><div id="counterseconds"></div><p>Seconds</p></div></div><div class="countertext">this is where some text goes</div></div>';
            $("body").html(getHtml);

Now if there is a space like this, then then it breaks:

    var getHtml = '<div class="counterwrap"><div class="countertitle"><h1><?php echo $ublunderconstructioninfo->title ; ?></h1></div><div class="counter"><div class="counternumbers"><div id="counterdays"></div><p>Days</p></div><div class="counternumbers"><div id="counterhours"></div><p>Hours</p></div><div class="counternumbers"><div id="counterminutes"></div><p>Minutes</p></div><div class="counternumberslast"><div id="counterseconds"></div><p>Seconds</p></div></div><div class="countertext">this is where some text goes

but when there is a linebreak

like this it breaks
</div></div>';
            $("body").html(getHtml);
Community
  • 1
  • 1
  • Look here: [How to create multiline strings][1]. [1]: http://stackoverflow.com/questions/805107/how-to-create-multiline-strings – Bgi Aug 29 '12 at 13:53
  • Put the backslash at the end of the line – Sathish Aug 29 '12 at 13:54
  • You all didn't understand his question! It is not a duplicate! The answer to your problem is: Use PHP and replace all "\n" in that string. You probably want to replace them with "
    " so the spaces will be shown on the page.
    – Display Name Aug 29 '12 at 14:00
  • @DisplayName thank you for your answer, it was the correct answer as it now works now i have replaces the \r\n with a
    – Robert Gouveia Aug 29 '12 at 14:08

1 Answers1

1

This is because strings in javascript can't be split across multiple lines.

Instead do something like:

var SomeString = "line 1\n" + 
   "line 2\n" + 
   "line 3";

Or

var SomeString = "line 1\
    line 2\
    line 3";
PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • how can i setup a var like you have said from a dynamic vairable called from the database. For instance i am calling the text which you can see from – Robert Gouveia Aug 29 '12 at 13:56
  • @RobertGouveia, normally I'd recommend asking this as a separate question, however I'm certain it's already been asked. If you're not satisfied by the answers provided, the correct next step is to open a bounty as per the [faq]. – zzzzBov Aug 29 '12 at 13:59
  • @zzzzBov i do not have a lot of rep as i am new, i didnt even know about this website until today when trying to search for my question. – Robert Gouveia Aug 29 '12 at 14:02
  • @RobertGouveia, then welcome to [SO]. I recommend thoroughly reviewing the [faq] thoroughly in addition to [editing-help](http://stackoverflow.com/editing-help). – zzzzBov Aug 29 '12 at 14:09
  • -1 Because it does not answer the actual question. – Display Name Aug 29 '12 at 14:10