I'm changing the HTML using javascript. Therefore, I have a String that saves the HTML code, since it is produced in a for-loop. In it there is an <a>
tag. To the <a>
tag I want to add an onClick
which calls a function with a parameter which is saved in a javascript variable.
let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window("+parameter+")'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;
This does not work because it would prodece the following html:
<a href='#' onClick='create_sprite_window(p1)'></a>
It does not work because there are no quotation marks before and after p1. My problem is that I would need a third kind of quotation marks in order to solve this. In the following example I will use # where I would need those quotation marks:
let parameter = "p1";
let to_html_String = "<a href='#' onClick='create_sprite_window(#"+parameter+"#)'></a>";
document.getElementById('sidebar_left_sprites').innerHTML = to_html_String;
I can't use a single quotation mark because it would end the onClick and I can't use double beacuse it would end to_html_String.
Are there third quotation marks or is there an other way to solve this?