here is my problem:
I have a div:
<div id="legend"></div>
and I want to fill it, under certain conditions, with this other code:
<p>Showing results....
Key:
<img src="/...."><=1
<img src="/..."><=2
<img src="/..."><=3
<img src="/..."><=4
<img src="/..."><=5
<p>
As you can see this is a legend and the main issue I think is that I have double quotes that cannot be easily included using for instance the solution here: Fill div with text
Thanks for your help!
var str= <p>write your complete code here</p>
$('#legend').html(yourconditionSatisfies?str:'')
By my understanding, you are trying to do something like this:
To insert HTML, you should use
.html()
, HOWEVER it would be far more efficient to just use Vanilla JS like so:Now, you also mention having problems with double-quotes. Well, there are two possible solutions.
1: Use single-quotes around your string:
'<img src="/..." />'
2: Escape the quotes:
"<img src=\"/...\" />"
And one more thing: If those newlines are actually in your HTML to insert, you can't have a multi-line string in JavaScript. You have two options here too:
1: Escape the newline:
2: Concatenate:
Assemble your html code in a variable according to your conditions (which we do not know), then use
html()
to set it to the element.As for dealing with the quotes, you can either use single quotes in variable, such as:
or you can escape the double quotes: