0

I have some occasions where I need to build a string via JavaScript to append to a HTML element.

My method has generally been to do something like this:

document.getElementById('theID').innerHTML=document.getElementById('theID').innerHTML+'<li>theString</li>';

and that works fine unless the appended string is advanced HTML that includes both JavaScript variables and text. In those cases it becomes a jumbled mess to maintain unless it still has the code spacing (whitespace).

Example of what I would like to have:

document.getElementById('theID').innerHTML=
document.getElementById('theID').innerHTML+
"<ul>
  <li><a href='"+varLink+"'>"+var1+"</a></li>
</ul>";

My problem is that it can't have it have any whitespace or I get an unterminated string literal on page load. Even a space after and before a '+' for concatenation causes the error. If I remove all whitespace from the string then it works fine, but like I said, it's hard to maintain/edit.

First off, is there a better way to do this through jQuery that won't generate that same error on page load? Barring that, is there some way to have the whitespace without getting the unterminated string literal?

7
  • are you saying that your above code works as is, but breaks if the innerHTML+'<li>...' is changed to innerHTML + '<li>...' ?? Commented Nov 17, 2011 at 14:08
  • It's more likely some embedded quotes are throwing it off. Commented Nov 17, 2011 at 14:10
  • @nickf - yes, that is what I'm saying. adding a space either within the concatenated string (i.e. within quotes) or before or after a plus symbol in the contatenated string causes the unterminated string literal. I edited my example to show what I mean. Commented Nov 17, 2011 at 14:14
  • @Dave Newton - yeah, I spent several hours the first time I had this happen chasing that down, but it really is the whitespace (or more specifically, spaces) in the code that is causing the unterminated string literal. If I take the same code and remove all spaces, it works fine. Commented Nov 17, 2011 at 14:14
  • @DaveNewton - clarification - if I remove all spaces outside the quotes and tags, it works. It's fine to have spaces within any HTML element outputted to the browser, but not if it is outside the tag (i.e. Whitespace) or apparently outside the quotes next to the concatenation symbol. Commented Nov 17, 2011 at 14:23

4 Answers 4

2

You could use a single \ to escape, but its not really a good idea as its not part of the standard;

var s ="<ul> \r\n\
  <li><a href='"+varLink+"'>"+var1+"</a></li> \r\n\
  bla bla bla \r\n\
</ul>";

The only other way is to concatenate each part of the string individually.
Using an array can be efficient/readable;

var buff = [];
buff.push("<li>...");
buff.push("</ul>");
document.getElementById('theID').innerHTML += buff.join("\r\n");
Sign up to request clarification or add additional context in comments.

1 Comment

Your buffer code will include the existing html twice since you use += to append the joined array. The \r\n seems unnecessary since as far as I can see the goal is just to make the code look neat.
1

Your problem isn't spaces, its line breaks in the middle of strings. You can get around that with \ but as Alex K mentioned, that is non standard.

For what its worth, I'd do something like this.

document.getElementById('theID').innerHTML += [ 
"<ul>",
  "<li><a href='", varLink, "'>", var1, "</a></li>",
"</ul>"
].join('');

Create an array, join the values and append them to innerHTML. You can add your whitespace for presentation without errors.

P.S. This is essentially Alex K's answer with some tweaks, I upvoted his.

2 Comments

Thanks for the explanation of the real cause of the problem. Although I still feel like there's got to be an easy way that I'm missing for just being able to type it like I want (maybe something like HEREDOC for PHP), both of your explanations give me a way to get it done without driving myself nuts trying to support it later. Thanks!
And after I said my heredoc thing, I found this other Stack Overflow session which has a discussion of other possible solutions: stackoverflow.com/questions/805107/… Thanks, guys! I appreciate every one of you for reaching out.
0

I don't know if this answers your question, but well if you wanted space you need to use + the way i know of getting it to work is like

document.getElementById('theID').innerHTML += 
 document.getElementById('theID').innerHTML = 
 "<ul>"+
 "  <li><a href='"+varLink+"'>"+var1+"</a></li>"+
 "</ul>";

 }

2 Comments

There is something wrong here, mixing the += and the =, I'm not sure what that accomplishes.
@Hemlock it replaces reference to the first innerHTML like if you where using concatenation in a loop
0

The jQuery equivalent would be neater:

$('#theID').append(...);

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.