Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript code, unterminated string literal

Error console: unterminated string literal

$html='<li><div class="above">'+$question_number+ 'Question Title</div>

The JQuery code is:

$html='<li><div class="above">'+$question_number+ 'Question Title</div>
<div class="middle"> <input type="text" name="question'+$question_number+ '" size="55"/></div>
<div class="below">'+$question_number+ ' <input type="text" name="option'+$question_number+ '" size="6"/>'+$question_number+ '<input type="text" name="option'+$question_number+ '" size="6"/>
   <input class="btn" type="button" name="Submit" value="Add" />
   <input class="btn" type="button" name="Submit" value="Remove" />
   </div>
</li>';

I know the value of $html is long, but how can I escape the trap of "unterminated string literal"? Is there a better to work around this problem?

like image 520
Steven Avatar asked Nov 14 '09 05:11

Steven


People also ask

What does unterminated string literal mean?

"unterminated string literal" means that somewhere a string variable is opened by not closed properly, either because of un-escaped character in it, or a line break, or something like that.

Is string a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.


2 Answers

Your lines need the continuation character if you're going to run the string literals over multiple lines. Put a "\" character at the end of each line or use string concatenation. In other words, you could turn the incorrect:

$html='<li><div class="above">' + $question_number + 'Question Title</div>
<div class="middle"> ... ';

into:

$html='<li><div class="above">' + $question_number + 'Question Title</div>\
 <div class="middle"> ... ';

or:

$html='<li><div class="above">' + $question_number + 'Question Title</div>' +
    ' <div class="middle"> ... ';

You don't actually need your HTML to be nicely formatted but, if you really want it in a form that you can print nicely, you can put embedded newlines into it as well:

$html='<li><div class="above">' + $question_number + 'Question Title</div>\n' +
    '<div class="middle"> ... ';

For readability of the code, I would use something like:

$html = \
    '<li>' +
    '  <div class="above">' + $question_number + 'Question Title</div>' +
    '  <div class="middle">' +
    '    <input type="text" name="question' + $question_number+ '" size="55"/>' +
    '  </div>' +
    '  <div class="below">' + $question_number + 
    '    <input type="text" name="option' + $question_number +
    '      " size="6"/>' + $question_number +
    '    <input type="text" name="option' + $question_number + '" size="6"/>' +
    '    <input class="btn" type="button" name="Submit" value="Add" />' +
    '    <input class="btn" type="button" name="Submit" value="Remove" />' +
    '  </div>' +
    '</li>';

and then pass it through a compressor to get a much shorter version for distribution.

like image 121
paxdiablo Avatar answered Nov 09 '22 18:11

paxdiablo


You could always just concatenate parts of it on multiple lines like:

$html='<li><div class="above">'+$question_number+ 'Question Title</div>';
$html+= '<div class="middle"> <input type="text" name="question'+$question_number;   
$html+=' size="55"/></div>';

etc.

like image 32
Samuel Avatar answered Nov 09 '22 18:11

Samuel