7

I have a comment system in which I want to realize inline-editing (when someone knows a good plugin or something similar please don't hesitate to give me a name) and found a Javascript snippet which replaces the text with a textarea and the text as the value of that textarea.

But now I need to add a button (submit button) to that textarea so that the user could save the text he edited.

My code looks now like

<span id="name">comment</span>

<div onclick="replacetext();">test</div>

<script type="text/javascript">
    function replacetext(){
            $("#name").replaceWith($('<textarea>').attr({ id: 'name', value: $('#name').text() }));
    </script>

I've tested it out with $("#name").append('<button>yes</button>'); but it didn't work.

4
  • 2
    Aside from a missing } in your example, it works fine here jsfiddle.net/j08691/adb8X Commented Dec 2, 2012 at 15:15
  • thanks but what code is required to add an extra button? jsfiddle.net/ZaEDw is not working Commented Dec 2, 2012 at 15:23
  • A better approach would be use .show(), and .hide() to display the TEXTAREA instead of the DIV. Or alternatively, have a class name on BODY which controls display of various elements on the page, and then set this class. Commented Dec 2, 2012 at 15:59
  • Corrected your jsFiddle: jsfiddle.net/adb8X/5 Commented Dec 2, 2012 at 18:39

4 Answers 4

2

The solution can be tried out using the following jsFiddle: http://jsfiddle.net/adb8X/5/

The line I believe you are after is:

  $('<button>yes</button>').insertAfter("#name");

The code above inserts a newly created DOM element (yes) right after the DOM element with the specified id in the target selector ("#name").

More about insertAfter here: http://api.jquery.com/insertAfter/

If you want to insert it into replacetext(), it will become:

function replacetext() {
    $("#name").replaceWith($('<textarea>').attr({
        id: 'name',
        value: $('#name').text()
    }));

    $('<button>yes</button>').insertAfter("#name");

} 

Note: I also corrected your jsFiddle. Please check here: http://jsfiddle.net/adb8X/5/ (There were problems with the settings and a small typo if I recall correctly). The corresponding line in that is:

 $("#name").append( $('<button>hi</button>') );
Sign up to request clarification or add additional context in comments.

Comments

2
function replacetext() {

    $("#name").replaceWith($('<textarea>').attr({
        id: 'name',
        value: $('#name').text()
    }));
   $('#name').after('<button id="test">test</button>');
}

$('#test').live("click",function(){
   alert("I am a newly-created element and .click won't work on me.");

});

You can't use .append() in a textarea because you can't "insert content" or append to it (there are other workarounds for that). You can do that in DIV, paragraph tag, or whatever that can act as a container.

http://api.jquery.com/append/
http://api.jquery.com/after/
http://api.jquery.com/live/ or .on() http://api.jquery.com/on/

Comments

1

Here is the working code.

<span id="name">comment</span>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<div onclick="replacetext();">test</div>

<script type="text/javascript">
    function replacetext(){
            $("#name").replaceWith($('<textarea>').attr({ id: 'name', value: $('#name').text() }));
            $('#name').after('<input type="submit" />');
    }
    </script>

Accept it as answer if it works

Comments

1

Another option that will let you replace labels that are generated dynamically, not just the one with the static id = "Name" for example:

html for a label or an anchor link in this example to be replaced with textbox:

<a href="#" onclick="editOpen(this);">@Html.DisplayFor(modelItem => Model.Name)</a>|

Javascript ( inside of the editOpen function)

function editOpen(ctrl) {
   //textbox
    var editText = $('<input>').attr({
        type: 'text',
        value: $(ctrl).text()
     });

    //edit button
    var saveEditLink = $('<input>').attr({value:'Save',type:'button'});

    //Put them together
    $(ctrl).replaceWith($(editText).after($(saveEditLink)));

}

1 Comment

thank you very much for your answer which was very interesting (also for future projects).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.