Js single and double quotes conflict problem resolution

  • 2020-03-26 21:23:27
  • OfStack

How to solve the conflict between single lead and double lead in js, think of the following code:


html += ' <a onclick="return removeOpenCss('+e.point.lng+e.point.lat+')"> cancel </a>';
 

This is the code in js, if I write it like this, it will prompt js error, because the parameter in removeOpenCss method does not have single or double lead, if I write it like this:


html += ' <a onclick="return removeOpenCss( " '+e.point.lng+e.point.lat+' " )"> cancel </a>';

I will directly report an error, because there is a conflict between single and double leads, so I only solved it by:


html += ' <a onclick="return removeOpenCss("'+e.point.lng+e.point.lat+'")"> cancel </a>';

In JS, errors are common when two double quotes are nested
The solution can be as follows
Replace the inner single quote with '
Replace the quotes with ".
The following example will do

<html>
<head>
<title>
JS Single quotes double quotes problem 
</title>
<script>
function showmSG(S)
{ 
alert(S);
}
</script>
</head>
<body>
<input type="button" value="Click Me" onclick="showmSG(''"FSDFDS')" />
<input type="text" id="txtName" name="txtName" value="'"FSDFDS" />
</body>
</html>


Related articles: