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 ?
0
give a positive ratinggive a negative rating
03 Feb 2021 at 06:09 PM
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>";

0
give a positive ratinggive a negative rating
14 Feb 2021 at 04:35 PM
Tim
Share on FacebookShare on TwitterShare on LinkedInSend email
x
x
2024 AnswerTabsTermsContact us