html code:
<ol id="ol_group" class="list-group list_of_items">
<li class="list-group-item completed_item">
<div class="text_holder">
how are you?
</div>
<textarea rows="" cols=""></textarea>
<div class="btn-group pull-right">
<button class="delete btn btn-warning">Delete</button>
<button class="edit btn btn-success">Edit</button>
</div>
<div class="checkbox">
<label> <input type="checkbox" class="pull-right"></label>
</div>
</li>
</ol>
js:
$(document).on('click', '.btn-group .edit', function(){
//alert("asdfsd");
var divedit = $(this).parent().prev();
if (!divedit){
return;
}
if (divedit.children("input").length > 0) {
return;
}
var mtext = divedit.text().substring(0, divedit.text().length-10);
var inputIns = $("<input type='text'/>"); //Create an input input box
var oldtext = divedit.html(); //Save the original value
inputIns.width(divedit.width()); //Set INPUT to be the same as DIV width
inputIns.val(mtext);
divedit.html(""); //delete the original cell DIV content
inputIns.appendTo(divedit).focus().select(); //Insert the input box code that needs to be inserted into the DOM node
inputIns.click(function () {
return false;
}); //Handle carriage return and ESC events
inputIns.keyup(function (event) {
var keycode = event.which;
if (keycode == 13) {
var newText = oldtext.replace(mtext, $(this).val());
divedit.html(newText); //Set new value
}
if (keycode == 27) {
divedit.html(oldtext); //return the old value
}
}).blur( function (event) {
if($(this).val() != oldtext){
var newText = oldtext.replace(mtext, $(this).val());
divedit.html(newText); //Set new value
}else{
divedit.html(oldtext);
}
} );
});