0

When I try to type text (between "") in javascript, I have to write everything on 1 line. I can't press enter because my code woudn't be valid then.

Appearantly you can't type on multiple lines in javascript. Is this correct or did I make a mistake?

Example:

var htmlcode = "<strong>29 juni 2013</strong> <br/> 22u";
//This works

var htmlcode = "<strong>29 juni 2013</strong> 
<br/> 
22u";
//This doesn't work
Matt
  • 1,835
  • 10
  • 31
  • 55
  • 1
    my eclipses gives me the + sign automatically when I press enter, since yours does not, see Chris's answer – Huangism Apr 10 '13 at 16:38
  • possible duplicate of [Creating multiline strings in JavaScript](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript) – PA. Apr 10 '13 at 16:39
  • This is weird, when I press the enter key next to my numpad, I don't get a "+". But when I press the enter key next to my delete key, it gives me a "+". Weird, but it's fixed, thanks! – Matt Apr 10 '13 at 16:40

2 Answers2

3

If you want to break up code to be on multiple lines, what you can do is either of the following:

// Concatenate to the string
var htmlcode = "<strong>29 juni 2013</strong> "
    + "<br/> "
    + "22u";

or

// Use backslashes at the end of each line. May not be supported everywhere,
//   so I'd avoid this approach.
var htmlcode = "<strong>29 juni 2013</strong> \
<br/> \
22u";
Chris Forrence
  • 9,860
  • 11
  • 47
  • 62
1

Inside of Eclipse, there is an option that could help. Go to Preferences -> JavaScript -> Editor -> Typing. On the page, enable "Wrap automatically" and "Escape text...". This will ensure that if you press enter while inside of a string, quotes, \n, and + will be added appropriately.

Andrew Eisenberg
  • 27,549
  • 9
  • 90
  • 141