0

I have double quotes as well as single quotes in a TD element. On click of that TD I'm passing value inside that TD to the function and create input element.
For the single quotes it's working fine, but in case of double quotes it's removing string after the double quotes.

Have a look at this fiddle, I tried escape and unescape but that also not working, I also tried replace function.

$(".table td").click(function(){
    var input = createInput($(this).html());
    $(this).html(input);
});

createInput = function(str){
    str = typeof str !== 'undefined' ? str : "";
    input = '<input value="'+str+'">';
    return input;
}
Martijn
  • 15,791
  • 4
  • 36
  • 68
www.amitpatil.me
  • 3,001
  • 5
  • 43
  • 61

4 Answers4

1
$(".table td").click(function() {
    var input = createInput($(this).html());
    $(this).html(input);
});

createInput = function(st){
    st = typeof st !== 'undefined' ? st : "";
    input = $('<input type="text">').val(st);
    return input;
}
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
1

Check this http://jsfiddle.net/F68bm/6/

$(".table td").click(function(){
    var input = createInput($(this).html());
    $(this).html(input);
});

createInput = function(str){
    //alert(str);
    str = typeof str !== 'undefined' ? str : "";
    input = '<input value='+str+'>';
    return input;
}

What was problem was your Quoting , so proper is

input = '<input value='+str+'>'

Consider it as 3 parts

1) < input value= ,Here it is static part ,evaluated as it is in HTML

2) +str+ Here this is dynamic part added after processing .

3) > ,This is also static part ,put as it is in HTML ,

Its simple :)

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0

You can escape quotes to stop them from ending your string prematurely.

/* javascript */

var str = "My string is \"awesome\".";

console.log(str);        //prints: My string is "awesome".
isNaN
  • 69
  • 3
0

For php language this htmlspecialchars function useful to display double/single quotes ( " / ' ) in textbox

PHP Example :

<!-- For PHP -->
<input type="text" value="<?=htmlspecialchars('YOUR DOUBLE " /SINGLE ' QUOTES STRING')?>">

For JS you can refer this link HtmlSpecialChars equivalent in Javascript?