-1

I want to escape special characters using Javascript. I used code snippet from this URL which http://jsperf.com/encode-html-entities. It works for <>& but not working properly for double quotes("). Javascript returns '\"' as """. It doesn't work. Am not able to figure out what wrong am doing.

Lot of edit requests- I am trying to pass value from one text box for popup to another using jQuery-

$('#contactDetails tr:last').before('<tr><td width="30%">' + $("#contactType option:selected").text() + ': </td><td width="70%"><div class="ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-a"><input type="text" name="email" id="email" class="ui-input-text ui-body-a contact-details limit-thirtyfive" value="' + $('#contactValue').val().escape() + '" data-id="' + randomTelecomId + '"/></div></td><td><a href="#" class="delete_contact" style="margin-right: 2px !important" data-id="' + randomTelecomId + '"><span class="glyphicon glyphicon-remove-circle delete-icon"></span></span></td></tr>');
$("#popupAddContactDetails").popup("close");

//overriding prototype function to escape characters
String.prototype.escape = function() {
    var tagsToReplace = {
        '&': '&amp;',
        '<': '&lt;',
        '>': '&gt;',
        '"':  '\"',
    };
    return this.replace(/[&<>"]/g, function(tag) {
        return tagsToReplace[tag] || tag;
    });
};
nickalchemist
  • 2,211
  • 6
  • 31
  • 58
  • `' " '` is this not working? – guradio Sep 23 '15 at 07:06
  • if you're trying to pass a textbox value's to another, why would you need this? can you paste us some code? – Buzinas Sep 23 '15 at 07:13
  • 1
    Regarding your comment on the deleted answer, you'd rather ask "how to transfer data from one textbox to another". Currently this is a [XY problem](http://meta.stackexchange.com/a/66378) because moving data in js should not require you to escape it (if done properly). Example: https://jsfiddle.net/gfyokwmm/ – Yoshi Sep 23 '15 at 07:13

3 Answers3

2

You can use '"' : '&quot;'

Shel Yang
  • 613
  • 1
  • 5
  • 9
1

dbl-quotes entity is &quote;, you can search html entities for more

XXLIVE
  • 156
  • 7
0

function escape(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }