6

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?

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Genius! Your suggestion is great!
1

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.

Comments

0

You could use arrays to store fragments of your markup and then use the join method to get the complete markup:

a = new Array(20);
a[0]='<li><div class="above">'
a[1]= $question_number
a[2]= 'Question Title</div>'
a[3]= '<div class="middle">'
a[4]= '<input type="text" name="question'
a[5]= $question_number
a[6]= '" size="55"/></div>'
a[7]= '<div class="below">'
a[8]= $question_number
a[9]= '<input type="text" name="option'
a[10]=$question_number
a[11]= '" size="6"/>'
a[12]=$question_number
a[13]='<input type="text" name="option'
a[14]=$question_number
a[15]='" size="6"/>'
a[16]='<input class="btn" type="button" name="Submit" value="Add" />'
a[17]='<input class="btn" type="button" name="Submit" value="Remove" />'
a[18]='</div>'
a[19]='</li>'

$html = a.join('');

Comments

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.