www.amitpatil.me www.amitpatil.me - 4 months ago 21
Javascript Question

Printing double quote in textbox in javascript

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;
}

Answer

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 :)

Comments