Javascript alert box - display single quotes or apostrophe

I have a problem with displaying of single quote character or apostrophe in javascript alert box. I am using combination of single and double quotes in related PHP / HTML. What should I do to be sure that single quotes will appear in alert box notification ?
03 Feb 2021 at 06:09 PM
0pnshow more
onefiveonefive
Hi,

The situation when using mulitple combinations of single and double quotes in PHP is quite common. To show javascript Alert notification with single quote, you will need to use escaped quotes and addslashes() function in your solution.

This combination of regular and escaped single and double quotes should work:

$notification = "'Content in single quotes'";

echo "<a href=\"javascript: void(0);\" onClick=\"javascript: alert('".addslashes($notification)."');\" >Show notification</a>";

14 Feb 2021 at 04:35 PM
0pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook & Twitter
2022 AnswerTabsTermsContact us