Inline editing as it’s known, or being able to edit content directly on a page is a great tool have under your belt. I’m going to run through exactly how it works as well as how to POST the updated text content through to a PHP script for server side processing to put in the database.
It looks a little complex so i’m going to quickly run through exactly what’s going on.

$(document).ready(function () {
function slideout() {
setTimeout(function () {
$("#response").slideUp("slow", function () {});
},
2000);
}
$(".inlineEdit").bind("click", updateText);
var OrigText, NewText;
$(".save").live("click", function () {
$("#loading").fadeIn('slow');
NewText = $(this).siblings("form").children(".edit").val();
var id = $(this).parent().attr("id");
var data = '?id=' + id + '&text=' + NewText;
$.post("update.php", data, function (response) {
$("#response").html(response);
$("#response").slideDown('slow');
slideout();
$("#loading").fadeOut('slow');
});
$(this).parent().html(NewText).removeClass("selected").bind("click", updateText);
});
$(".revert").live("click", function () {
$(this).parent().html(OrigText).removeClass("selected").bind("click", updateText);
});
function updateText() {
$('li').removeClass("inlineEdit");
OrigText = $(this).html();
$(this).addClass("selected").html('<form ><textarea class="edit">' + OrigText + '" </textarea> </form><a href="#" class="save"><img src="images/save.png" border="0" width="48" height="15"/></a> <a href="#" class="revert"><img src="images/cancel.png" border="0" width="58" height="15"/></a>').unbind('click', updateText);
}
});
In this example I haven’t added the database connection script. We basically POST the data to update.php and using PHP we echo back the results. You could quite easily add this data to the database.
$text = mysql_escape_string($_POST['text']); $id = mysql_escape_string($_POST['id']); echo $text; // this is where you could add the updated text to the database where the database row id = $id for example.
The HTML is simply a list item with the class ‘inlineEdit’ this makes the content of the element editable. The more elements that you add with this class, the more you can edit. notice how i’ve added an id of 1 to the example below. This could be a specific row in a database for example if you have multiple editable elements on a page.
<h2>jQuery Inline-Edit <img id="loading" src="images/loading.gif"/></h2> Click the text below to edit it. <div id="response"></div> <ul> <li class="inlineEdit" id="1">Lorem Ipsum....</li> </ul>
jesus…great work, always wanted to make something like this…! :]
nice tutorial, great job man.
visit http://www.leaneveryday.net for asp.net and jquery tutorials.
Can i changed the textarea to textfield???
A good tutorial, question is what if there are two fields that will be in the edit in the database. Do you have to make two javascript code for each field ?, or with one javascrip code can change the data in each field? Can you can explain it ?, Thanks.
How do i make sure that the edited content is validated before it is saved. i want both a client side and php server side script please. Am using it for my final year project
I like this but is it possible to have multiple response boxes for multiple editable areas?
For example I loop 10 rows of data which works great. I get 10 different boxes I can edit. The only thing is t he response and loading image always show up on the first one on the page. I can’t think of a way to do this?
The code below is what I loop. Thanks
<div class="inlineEdit" id="”>
I finally got it working like I wanted.
I changed this -
var data = '?id=' + id + '&text=' + NewText;
$.post("update.php", data, function (response) {
to this
$.post("update.php", { 'id' : id, 'text' : NewText }, function (response) {
Спасибо за добавление. Очень помог Ваш пост.
I’m having a little trouble. For some reason the id isn’t being posted to the php script.
I don’t really know jQuery that well and can’t seem to figure out the problem myself. Any help you can provide would be great.
Thanks.
Great work!
My web app is virtually being built off this blog… drag & drop ajax sort… will use “username availability” next… will use this post next too. please keep making tuts like these, its a good blog for the average web developer. absolutely invaluable posts.
I really need to read through all of your old posts, too… but if this blog has RSS I can port all the posts to my Ipod touch i could read them all offline on journies.
This is amazing, I love all the new ideas you come up with!
Thank you
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
14 discussions around jQuery and PHP inline editing