Reputation: 257
I want to create a tag like <?xml version="1.0" encoding="UTF-8" standalone="no"?>
on button click, so i have created a simple function to achieve this
function onclick(){
var element = getStringBuilder();
element.append("<?xml version=" + "1.0" + " encoding=" + "UTF-8" + " standalone=" + "no" + "?>");
element = element.toString();
}
function getStringBuilder () {
var data = [];
var counter = 0;
return {
// adds string s to the stringbuilder
append: function (s) {
data[counter++] = s;
return this;
},
toString: function (s) { return data.join(s || ""); }
}
}
But i am getting output as below
<?xml version=1.0 encoding=UTF-8 standalone=no?>
, i want "1.0", "UTF-8", "no" as string in the tag. How can i achieve this
Thanks in advance
Upvotes: 1
Views: 2067
Reputation: 5705
JavaScript uses " to separate the strings. Use ' and inside this, use ". Like this:
element.append('<?xml version="1.0" encoding="UTF-8" standalone="no" ?>');
Upvotes: 0
Reputation: 324650
In this case, just use different quotes:
element.append('<?xml version="1.0" encoding="UTF-8" standalone="no"?>');
In the more general case, escape:
element.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
Either way, concatenating is not appropriate here.
Upvotes: 1
Reputation: 128791
What you're currently doing doesn't have any quotation marks within the string. "a" + "b"
concatenates the strings containing the characters a
and b
together to make "ab"
, not "a""b"
.
The easiest way to do this is to wrap your entire string in single quotes ('
) and retain the double quotes ("
) within your string:
element.append('<?xml version="1.0" encoding="UTF-8" standalone="no"?>');
Another method is to escape your double quotes to make JavaScript treat them as part of the string:
element.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
Upvotes: 2
Reputation: 36438
By actually including them in the string?
element.append('<?xml version="1.0" encoding="UTF-8" standalone="no"?>');
Upvotes: 1