Skip to content Skip to sidebar Skip to footer

How To Pass Quotation Marks To Javascript Function

I have a javascript function which accepts strings which include quotation marks and displays in a input field. But this function does not accept the string after the quotation mar

Solution 1:

You must escape special HTML characters like ", <, >. Eg:

functionsearchFormatter(cellvalue, options, rowObject) {
    cellvalue = cellvalue.
                replace(/&/g,'&amp;').
                replace(/>/g,'&gt;').
                replace(/</g,'&lt;').
                replace(/"/g,'&quot;');
    return'<input id="txt' + cellvalue + '" type="text"  disabled="disabled"  value="' + cellvalue + '" />';
}

Solution 2:

You have to pass the value of the cellValue as shown below ( var cellValue = 21"(Should be given as 21 followed by &quot followed by ;(semicolon))

You can get the value as 21" in the input box. Hope this helps.

Solution 3:

You need to replace " with &ampquot;

Use this helper method:

functionhtmlEncodeInputDisplay(string) {
    if (string == null || jQuery.trim(string) == "") returnstring;
    string = string.replace(/&lt;/g, "<");
    string = string.replace(/&gt;/g, ">");
    string = string.replace(/&amp;/g, "&");
    string = string.replace(/&nbsp;/g, " ");
    string = string.replace(/\"/g, "&quot;");
    returnstring;
}

Solution 4:

just escape the value when creating the input element, and then unescape the value to display it correctly.

Simplified example:

functionmake_input(val) {
    return'<input value="' + escape(val) + '" />';
}

// add to dom

$('input').each(function(){
   var current_value = $(this).val()
   var new_value = unescape(current_value)
   $(this).val(new_value)
})

Solution 5:

You can use the Javascript Functions escape() to encode a sting and then unescape() to decode them if needed.

You can also manually escape them like in the example below.

var name = prompt("Please write your \'name\' in the box\r Write it with \"quotes\" like this.","");

The quote escaped using \ goes through as a part of the string.

Post a Comment for "How To Pass Quotation Marks To Javascript Function"