4

When I put Single quote in text box, it does not show in dynamically added textbox. See following example :

$("#abc").before("<div><input type='text' value='" + $scope.txt + "'/></div>");

http://jsfiddle.net/U3pVM/15777/

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Sunil Madan
  • 457
  • 1
  • 4
  • 17

5 Answers5

4

I've had a similar problem some time ago, and found this answer https://stackoverflow.com/a/9756789/2619170

See the quoteattr function, that's what you need: http://jsfiddle.net/U3pVM/15780/

function quoteattr(s, preserveCR) {
    preserveCR = preserveCR ? '&#13;' : '\n';
    return ('' + s) /* Forces the conversion to string. */
        .replace(/&/g, '&amp;') /* This MUST be the 1st replacement. */
        .replace(/'/g, '&apos;') /* The 4 other predefined entities, required. */
        .replace(/"/g, '&quot;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        /*
        You may add other replacements here for HTML only 
        (but it's not necessary).
        Or for XML, only if the named entities are defined in its DTD.
        */ 
        .replace(/\r\n/g, preserveCR) /* Must be before the next replacement. */
        .replace(/[\r\n]/g, preserveCR);
        ;
}
Community
  • 1
  • 1
ToX 82
  • 1,064
  • 12
  • 35
0

Try this, I have assumed that you want "text with quote" as an input to be added and displayed without quotes.

 function TodoCtrl($scope) {
  $scope.txt = "";
    $scope.addBefore = function() {
        $("#abc").before("<div><input type=\"text\" value=" + $scope.txt + "></div>");
    };
}
Newinjava
  • 972
  • 1
  • 12
  • 19
0

Try replacing _'_ by <code>&ampapos;<code> , or _"_ by <code>&ampquot;<code>

JSFiddle

    function TodoCtrl($scope) {
     $scope.txt = "";
     $scope.addBefore = function() {
        $("#abc").before("<div><input type='text' value='" + $scope.txt.replace("'","&apos;") + "'/></div>");
    };
}
ketan
  • 19,129
  • 42
  • 60
  • 98
Shanavas M
  • 1,581
  • 1
  • 17
  • 24
0
function TodoCtrl($scope) {
  $scope.txt = "";
    $scope.addBefore = function() {
        $("#abc").before('<div><input type="text" value="' + $scope.txt + '"></div>');
    };
}

just change from double to single

http://jsfiddle.net/U3pVM/15784/

Muhaimin
  • 1,643
  • 2
  • 24
  • 48
0

I have done this by replacing the single or double Quote by equivalent HTML code.

Sunil Madan
  • 457
  • 1
  • 4
  • 17