I-Am-Bot Code, technology and life

Editable.js

There are many excellent scripts to edit something inline using javascript (like jeditable), but not many that work standalone. If you hate wanting to include a 50 KB library just for this simple functionality, you are in the right page. This script is tiny (2.2 kB minified) and works on most modern browsers.

Demo Download

Steps

1) Include the editable.js file in your page

2) Add the attribute class="editable" to a division, paragraph or any other non-editable element (input boxes, textareas don't work!)<td>. If you want the cells of a table to be made editable, simply add the class="editable" to the table element and NOT to the table cells (td element) themselves

4) At the bottom of the page, initialize the script by adding EDITABLE.init(); within the script tag.

5) If you want the updated value to be updated in the server, simply add the url to which the data must be sent within the init function:

EDITABLE.init('http://example.com/update.php');

The ID of the element that was updated, and the updated value will be sent as parameters with the URL (GET) in this form:

http://example.com/update.php?id=div1&value=newvalue

Please note that you will have to sanitize the updated value before storing in the database!

6) Click on the element for it to turn into a text box. Update the value, and press enter to save. Press tab or click anywhere on the page to discard updates and revert to the old value

Changelog
Version 0.4

  • Support for select boxes. The select options have to be initialized before EDITABLE.init() is called
  • Support for setting a value that is returned from the server after updating
  • Few minor bugs fixed, and some code cleaned up

Version 0.3

  • The script is now in its own namespace EDITABLE
  • Initialize the script by calling EDITABLE.init() at the bottom of the page
  • Passing a URL in the init() method will send the ID and updated value of the element to a server side script using AJAX/

Version 0.2.1

  • Fixed bug in IE

Version 0.2

  • Support for all major browsers
  • Table cell editing more robust
  • Added support for IE 7 and 8. Uses ie.js for support for getElementsByClassName

Initial Release

  • The editable box now fits to the size of the original element. You can see that in the demo
  • Support for inline table cell editing is now included (Proper layout/orientation only work in FF for now. Horribly out of place in other browsers and will be fixed later)
  • Editing of an element with other HTML tags now works better

Gotchas:

1) The size of the editable box doesn't fit to the size of the original element when the DOCTYPE tag is added to the HTML page.

The text box doesn't fit to the size of the original element (will be fixed)

Tested On:

Windows - Firefox 3.6, IE 7 & 8, Chrome

Mac - Firefox 3.6, Safari 4, Chrome

Terms:

Free to use for personal or commercial purposes, but please give credits

Notes:

I know it can always be better, and I'm just a beginner. So comments, suggestions always welcome!

Comments (15) Trackbacks (0)
  1. Hi, this is exactly what I’m looking for. However, right now your script only saves on the client side. I looked at the code and see that you have functions to save to database, but I’m confused how to config it. Would you like to include some documentation how to implement such a thing? Thanks!

    • @ Esente, thanks for your comments. Yes, the script sends the updated value to a server side script that you mention. Like its there in the description, it sends the id of the element and the updated value to the URL as GET parameters. If you are in php, you can simply get this new value as:

      $var = $_GET['id_of_element'];

      You can then update this value in the database

  2. Thanks for the script. works great in my case. I modified the script to use prototype.js with elements = $$(‘.editable’). For the span element, the edit box is offset several pixels above the element. Haven’t found a way to fix that.

  3. Thank you for the script. How difficult would it be for the server to prepare data to be redisplayed back in the table? For example, you change a quantity field which would send the value to the server. Then the server prepares a new total to be redisplayed in the table.

  4. Also, with IE 8 I’m getting the following error when my table page renders:

    Object expected
    editable.js line: 44
    Code: 0 Char: 5


Leave a comment

(required)

No trackbacks yet.