Reputation: 939
On html page if I give name in double quotes then it is not getting reflected on page. It displays a blank string. I tried with escape() function but that didn't work. So what is the way to display a string in double quotes.
One thing I forgot to mention that I want to display the string in input text box.
Upvotes: 22
Views: 92997
Reputation: 196
Whoever is still looking into this should use the following:
In your HTML page/template, use this code (in this case, he wants it for an input)
<input id="name" name="label" type="text" value="<%- label %>"
Notice that I'm using
<%- %>
instead of the classic
<%= =>
That should automatically escape the value you're trying to display and show up in your input without any extra work.
Upvotes: 1
Reputation: 939
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.
Upvotes: 2
Reputation: 499
to show double quote you can simple use escape character("\") to show it.
alert("\"Hello\"");
Upvotes: 11
Reputation: 5108
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
Upvotes: 30
Reputation: 11798
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 "
Upvotes: 3