Reputation: 5376
I am creating a button dynamically through javascript and trying to pass a string as argument to the onclick function as below.
let control = '<li class="buttons-container">';
control += `<button class="gray-button" onclick="diagBtnClick('${yesStepIntent}', '${yesBtnLbl}')">${yesBtnLbl}</button>`;
In the above code, I am passing 2 arguments to onclick function. yesBtnLbl value is coming from server. If it doesn't contain any single quote character with in a string, it is working fine. For example, yesBtnLbl = 'I will' is working fine. But yesBtnLbl = "I'll" is not working. Even I tried with yesBtnLbl = "I'll" and also yesBtnLbl = 'I'll'; which is also not working. Every time it is throwing error 'Uncaught SyntaxError: missing ) after argument list' when I tried to hit the button if the string contains single quote character within the string.
Can anyone please help me to fix this issue.
Upvotes: 1
Views: 702
Reputation: 1021
In instances like this you can try changing your quotes around;
control += "<button class='gray-button' onclick='diagBtnClick("${yesStepIntent}", "${yesBtnLbl}")'>"${yesBtnLbl}"</button>";
But this will break if you use "
in your variable.
EDIT: Or you can try;
control += `<button class='gray-button' onclick='diagBtnClick("${yesStepIntent}", "${yesBtnLbl}")'>${yesBtnLbl}</button>`;
Upvotes: 2