How to display double quotes in JavaScript

橙三吉。 提交于 2019-11-30 01:51:38

You have several options:

var str = 'My "String"'; //use single quotes for your string
var str = "My \"String\""; //escape your doublequotes
var str = "My "String""; //use it as html special chars

to show double quote you can simple use escape character("\") to show it.

alert("\"Hello\"");

Try escaping it either with \" or, if this does not work, with \x22

Oh, and if you want to output HTML instead of using this inside a Javascript string, use "

Hello everyone thanks for suggestions. My issue has been solved now. I created one private method to which I passed my required parameters which contains double quotes in it. Here is the method.

 HTMLEncode:function(str){
              var i = str.length,
                  aRet = [];

              while (i--) {
                var iC = str[i].charCodeAt();
                if (iC < 65 || iC > 127 || (iC>90 && iC<97)) {
                  aRet[i] = '&#'+iC+';';
                } else {
                  aRet[i] = str[i];
                }
               }
              return aRet.join('');    
            },

The above method will convert double quote into the required format to display it on web page. I referred one of the stackoverflow page for this. Thanks. Hope this will help others.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!