1

I am using

$('#'+type+'name'+id).hide().html('ghj').fadeIn(150);

Which works fine. But when I use

$('#'+type+'name'+id).hide().html('
A lot of html code
').fadeIn(150);

The function does not work anymore. Since I want to keep the function tidy I want to put the HTML there in multiple lines. Why doesnt this work?

EDIT: Still does not work:

function Rename(type,id,content){
$('#'+type+'name'+id).hide().html(' '+
    '   <td height=\"20\" valign=\"middle\">'+
    '       <span onclick=\"RenameDo(\'notify\',\''+id+'\',''+content+'')\">'+
    '           <img title=\"Wijzigen\" class=\"LinkImage\" src=\"/common/images/save.png\">'+
    '       </span>'+
    '   </td>'+
    '   <td valign=\"middle\" rowspan=2>&nbsp;fghfgh</td>'+
' ').fadeIn(150);

}
2
  • If you don't write the code it's hard to guess what is going on. Commented Mar 9, 2013 at 19:05
  • Post your expected HTML output, without all the escaped quotes. Does the function RenameDo takes strings as arguments? Commented Mar 9, 2013 at 19:20

2 Answers 2

4

Just use + to concatenate long strings:

$('#'+type+'name'+id).hide().html(' ' + 
'A lot of html code' +
' ').fadeIn(150);

Or use \ to escape newline:

$('#'+type+'name'+id).hide().html('\
A lot of html code\
').fadeIn(150);

Update

For the specific string, use this:

function Rename(type,id,content){
    $('#'+type+'name'+id).hide().html(' '+
        '   <td height="20" valign="middle">'+
        '       <span onclick="RenameDo(\'notify\',\'' + id + '\',\'' + content + '\')">' +
        '           <img title="Wijzigen" class="LinkImage" src="/common/images/save.png">'+
        '       </span>'+
        '   </td>'+
        '   <td valign="middle" rowspan=2>&nbsp;fghfgh</td>'+
    ' ').fadeIn(150);
}
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks! So javascript really looks where I put the "enter"? Annoying!
@Mbrouwer88 Isn't that how every language processes strings?
@JanDvorak I actually got that from other Stack Overflow questions, including one that was no more than a few weeks ago. Google style guides recommend concatenation google-styleguide.googlecode.com/svn/trunk/… as the slash technique isn't part of ECMAScript.
Same comment as in @Kolink's answer, the escaped newline method can be pretty risky as any extra whitespace other than a newline immediately following the backslash will cause a SyntaxError: Unexpected token ILLEGAL
@JanDvorak I thought I had read somewhere that it was being brought in in ECMAScript 6, although I may be wrong and you're right that it's already included in ECMAScript 5 - I'm not a JavaScript expert. I was simply quoting from the Google document. The debate on the two methods rages if you look at other SO questions and articles on the subject. In either case, the slash method hasn't always been part of the standard and you should take care to test in all browsers you intend to support/receive significant traffic from. Which was my original point.
|
1

There are two ways to do this, the "hack way that works anyway", and the "proper way".

Hack way:

$("#"+type+"name"+id).hide().html("\
A lot of html code\
").fadeIn(150)

Explanation: By adding a backslash at the end of the line, you are effectively "escaping" the newline so it is ignored instead of an unterminated string constant.

Proper way:

$("#"+type+"name"+id).hide().html(
"A lot of html code\n"
+"Just adding some more\n"
+"Because whitespace outside of strings is fine."
).fadeIn(150)

7 Comments

Note the escaped newline is removed, not treated as a newline characted
I'm not sure I would call it a hack, as long as it's documented.
It's actually never been defined in any standard, and I think strict mode will fail on it.
The ES5 spec allows this explicitly, even in strict mode. SingleStringCharacter ::...\ LineTerminatorSequence
IMHO, the main problem with the first method -- escaped newlines -- is that any whitespace other than the newline, e.g. spaces, will cause a syntax error. It's pretty easy for extra white space to go unnoticed there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.