I am trying to concatenate html inside a variable. I was able to this successfully without any syntax error
htmlstr = "";
htmlstr += "<p>some paragraph";
htmlstr += "<p>another paragraph </p>";
$(list).html(htmlstr);
But when I try to this
htmlstr = "";
htmlstr += "<button class="first"></button>";
htmlstr += "<button class="second"></button>";
$(list).html(htmlstr);
I get an error "Uncaught syntaxerror: Unexpected Identifier from the browser. Am I doing this the wrong way?
I am trying to concatenate html inside a variable. I was able to this successfully without any syntax error
htmlstr = "";
htmlstr += "<p>some paragraph";
htmlstr += "<p>another paragraph </p>";
$(list).html(htmlstr);
But when I try to this
htmlstr = "";
htmlstr += "<button class="first"></button>";
htmlstr += "<button class="second"></button>";
$(list).html(htmlstr);
I get an error "Uncaught syntaxerror: Unexpected Identifier from the browser. Am I doing this the wrong way?
Share Improve this question edited Oct 9, 2013 at 0:51 Tushar Gupta - curioustushar 57.1k24 gold badges105 silver badges109 bronze badges asked Sep 26, 2013 at 15:46 lboyellboyel 2,2384 gold badges29 silver badges38 bronze badges 3-
Are first and second variables or the class names? If they are the class names, you have the escape the quotation marks,
\"first\"
– DFord Commented Sep 26, 2013 at 15:49 - 1 you're getting the error cause the quotes around the class names are terminating the string. That makes the word "first" unexpected. See Tushar's answer for a cleaner output – Kai Qing Commented Sep 26, 2013 at 15:50
- First and second are class names for clarification – lboyel Commented Sep 26, 2013 at 15:50
7 Answers
Reset to default 5your code
htmlstr += "<button class="first"></button>";
^ string ^ var ^ string ^
in your code first
is variable
so your getting error instead of string
correct way
htmlstr += '<button class="first"></button>';
^ ^ //changed to single quotes
htmlstr += '<button class="second"></button>';
In your current code, the quotes are terminating the string literal. You need to switch up or escape your quotes:
htmlstr += '<button class="first"></button>';
or
htmlstr += "<button class=\"first\"></button>";
Use "+" to concatenate variable and text
htmlstr = "";
htmlstr += "<button class="+first+"></button>";
htmlstr += "<button class="+second+"></button>";
$(list).html(htmlstr);
Or this if you have no variable but double and simple quote
htmlstr = "";
htmlstr += "<button class='first'></button>";
htmlstr += "<button class='second'></button>";
$(list).html(htmlstr);
Instead of use "
use '
for value of attribute
try this:
htmlstr = "";
htmlstr += "<button class='first'></button>";
htmlstr += "<button class='second'></button>";
htmlstr = "";
htmlstr += "<button class='first'></button>";
htmlstr += "<button class='second'></button>";
$(list).html(htmlstr);
You can't use "
inside "
. Use '
instead. This second (embeded) "
will be seen as the closing tag for that string, but it isn't. It is part of that string. So just use the alternative (but equally useful) '
.
The problem is that you are not escaping your quotes around class property value. My suggestions would be to use single quotes around your strings to eliminate the need for escaping double quotes:
htmlstr = '';
htmlstr += '<button class="first"></button>';
htmlstr += '<button class="second"></button>';
$(list).html(htmlstr);
If first and second are variables then you do waht Donovan Charpin said. However if it is the name of the class you can try this:
htmlstr = "";
htmlstr += "<button class='first'></button>";
htmlstr += "<button class='second'></button";
$(list).html(htmlstr);
Did not realize someone beat me to the punch on that.