-2

I have a link, when I click on it, it should select a div p tag contents and replace the p tag with text box and previously stored contents.

Now my code is removing the p tag and adding text box but it is removing text box immediately and inserting p tag. After user presses enter, I want ajax fn to be called.

This is what I tried,

<a href="" id="den">DEN</a>
<div id="test">
<p id="x">I am doing good</p>
</div>
 $("#den").live("click", function () {
       var con = $("#test >p#x").html();
       $("#test > p#x").remove();
       $("#test > p#x").append('<textarea type="text" value='+con+'></textarea>');

}); 
        });
ram
  • 1,111
  • 6
  • 15
  • 24

2 Answers2

3

After you remove #x, it's not in the DOM, so appending to it useless. If you want to replace the element's contents, use .html() instead. Also, <textarea>s have text content, not value attributes.

$("#den").live("click", function () {
    var con = $("#test >p#x").html();
    $("#test > p#x").html('<textarea type="text">'+con+'</textarea>');
});

Note, since element IDs must be unique, the selector #test > p#x is grossly overspecified, and is equivalent to simply #x. There's also a simpler, idiomatic way to accomplish this entire task:

$("#den").live("click", function () {
    $("#x").html(function (i, oldHtml) {
        return '<textarea type="text">'+oldHtml+'</textarea>';
    });
});

http://jsfiddle.net/mattball/PSV7A/

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

You can not make changes in removed element JSFIDDLE

Note . textarea does not have type attribute

and . Setting value of textarea would not show in box

<a href="" id="den">DEN</a>
<div id="test">
<p id="x">I am doing good</p>
</div>
 $("#den").on("click", function () {
   $("#test > p#x").html('<textarea >' +  $("#test >p#x").html() +'</textarea>');
 return false;

});

Anoop
  • 23,044
  • 10
  • 62
  • 76