I’m sure there is a simple answer to this but I couldn’t find it.
I know how to concatenate a normal string in JavaScript but how would I do it within an object?
I require this as I’m reading through an XML file and creating the HTML for a list of links. Each time I go through the loop I want to create a new <li> containing the link. How do I get the current value of the string and then append the new link on the end? Once I’ve read through the XML I’ll append the HTML to the page.
I’ve tried:
carParks.links = carParks.links + "<li><a href=\"#\">car park</a></li>";
with no success.
Any help is much appreciated.
String concatenation with an object property is just the same as anything else. Theoretically the code you have there should work, as long as
carParks.linksis a writeable property. When you perform string concatenation using the+or+=operators, except when using them as arithmetic operators, the operands are converted to strings. For example:If you’re getting an error message, check that
carParksis defined and is a JavaScript object with writeable properties (e.g. not part of an external interface). If you’re getting no error, make sure carParks.links is not a number. If this doesn’t help, please post a little more of the surrounding code and I’ll take another shot at it.