22

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.

pan1490
  • 939
  • 1
  • 7
  • 25

5 Answers5

30

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
MaGnetas
  • 4,918
  • 4
  • 32
  • 52
  • Ok but how would I know whether the string contains a double quote or not? – pan1490 Dec 17 '13 at 09:03
  • Well I'm not familiar with your problem enough but you can at least look for double quotes using string.indexOf('"') or any other way. – MaGnetas Dec 17 '13 at 09:07
  • You also might want to check this out: http://stackoverflow.com/questions/784586/convert-special-characters-to-html-in-javascript – MaGnetas Dec 17 '13 at 09:08
  • Hello MaGnetas. Thanks for help. I have posted one answer which was my requirement. Please go through that. Thanks :) – pan1490 Dec 18 '13 at 06:43
  • You see, the actual problem is that when you're setting input value it's done in between double quotes. So if your value also includes double quotes the limits of the value are unclear. That's why you need to replace them with something that would work as double quotes but wouldn't interfere with the rest of HTML. In php we have htmlspecialchars function for that. In my example above there's a javascript function that behaves the same way - replaces "special" characters with "alternative values". Your code might work too but is hard to read for humans :) – MaGnetas Dec 18 '13 at 06:56
  • Well I don't think the real programmers will face any issue regarding the function I have written. That function should not be a big deal for them :P And I tried your code also. It was partially working. I didn't get the full solution. (If you have bit knowledge of RESTful web services- I have removed unnecessary calls to REST for fast performance. I am using backbone.js . So whenever any data is getting updated,only update REST is getting called to save that value in the database and on web page the value for that field should immediately get reflected without any call to REST) – pan1490 Dec 18 '13 at 07:23
11

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

alert("\"Hello\"");
Code Breaker
  • 499
  • 1
  • 4
  • 19
3

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 "

Rob
  • 11,492
  • 14
  • 59
  • 94
  • Hello Robert. Thanks for help. I have posted one answer which was my requirement. Please go through that. Thanks :) – pan1490 Dec 18 '13 at 06:44
2

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.

pan1490
  • 939
  • 1
  • 7
  • 25
1

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.

Bruno
  • 196
  • 1
  • 7