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 = {
'&': '&',
'<': '<',
'>': '>',
'"': '\"',
};
return this.replace(/[&<>"]/g, function(tag) {
return tagsToReplace[tag] || tag;
});
};