Mar 24

Code Snippet: jQuery Edit In Place

There are many solutions to the edit-in-place problem, but I wanted to make an easy solution that wasn't as complicated as some of the other edit-in-place JavaScript scripts, like jEditable.


  1. Detects surroundings and keeps the input container as either a block or inline display.
  2. Highlights text if it is the original text. If the text has changed, the entire text is not highlighted on edit.
  3. Easy customizable and styleable.


Test Input: Click here to change this text.

JavaScript Code

(function($) {
        edit_in_place: function(opts, callback) {
            var self = this;
            var defaults = {
                'input_type': 'text'
            var options = $.extend({}, defaults, opts);

            return this.each(function() {
                var $this = $(this);
                var $input;
                var original_value = $this.html().replace(/<br.*?>/g, '\n');
                var original_display = $this.css('display');

                $this.bind('click', function() {
                    var starting_value = $this.html().replace(/<br.*?>/g, '\n');

                    if (options['input_type'] == 'text') {
                        $input = $.make('input', { type: 'text', name: 'eip_input', value: starting_value });
                    } else if (options['input_type'] == 'textarea') {
                        $input = $.make('textarea', { name: 'eip_input' }, starting_value);

                    var $form = $.make('div', { className: 'eip-container' }, [
                        $.make('button', { className: 'eip-submit' }, 'OK'),
                        $.make('button', { className: 'eip-cancel' }, 'Cancel')

                    $this.css({'display': 'none'});
                    if (original_value == starting_value) {

                    var restore_input = function(input) {
                        return function($this, $form) {
                            $this.css({'display': original_display});
                            if (input) {
                                $this.html(input.replace(/[\n\r]+/g, "<br /><br />"));
                                $.isFunction(callback) &amp;&amp;, input);
                        }($this, $form);

                    setTimeout(function() {
                        $(document).one('click.edit_in_place', function() {
                        $ {
                            if ( == 'eip-cancel') {
                            } else if ( == 'eip-submit') {
                            return false;
                    }, 10);



        make: function(){
            var $elem,text,children,type,name,props;
            var args = arguments;
            var tagname = args[0];
                if (typeof args[1]=='string'){
                    text = args[1];
                }else if(typeof args[1]=='object' &amp;&amp; args[1].push){
                  children = args[1];
                    props = args[1];
                if(typeof args[2]=='string'){
                    text = args[2];
                }else if(typeof args[1]=='object' &amp;&amp; args[2].push){
                  children = args[2];
            if(tagname == 'text' &amp;&amp; text){
                return document.createTextNode(text);
                $elem = $(document.createElement(tagname));
                    for(var propname in props){
                      if (props.hasOwnProperty(propname)) {
                            if($':input') &amp;&amp; propname == 'value'){
                            } else {
                                $elem.attr(propname, props[propname]);
                    for(var i=0;i&lt;children.length;i++){
                return $elem;


To use this code, simply use this HTML, CSS, and small JavaScript snippet:

<div class="eip">
    Test Input: <span class="eip-text">Click here to change this text.</span>

And this CSS:

.eip {
    font-family: Helvetica;
    font-size: 16px;

.eip .eip-text {
    font-weight: bold;
    padding: 2px 3px;
    border: 1px solid white;

.eip .eip-container {
    display: inline;

.eip input {
    font-family: Helvetica;
    font-size: 16px;
    font-weight: bold;
    padding: 2px;
    border: 1px solid #A0A0A0;
    display: inline;
    width: 250px;

And this simple piece of JavaScript, which includes a callback function that has the same scope as the original selectors:

$(document).ready(function() {
    $('.eip .eip-text').edit_in_place({}, function() {
        var $this = $(this);
        $this.animate({'backgroundColor': 'orange'}, {'duration': 300, 'queue': false, 'complete': function() {
            $this.animate({'backgroundColor': 'white'}, {'duration': 300, 'queue': false});

Note that I am animating background colors in this small JavaScript snippet. To animate colors, you need John Resig's excellent jQuery.color.js.


Comments are closed.

That's all the comments I'm taking for this post. Email me your thoughts:

Samuel Clay is the founder of NewsBlur, a trainable and social news reader for web, iOS, and Android. He is also the founder of Turn Touch, a startup building hardware automation devices for the home. He lives in San Francisco, CA, but misses Brooklyn terribly. In another life in New York, he worked at the New York Times on DocumentCloud, an open-source repository of primary source documents contributed by journalists.

Apart from NewsBlur, his latest projects are Hacker Smacker, a friend/foe system for Hacker News, and New York Field Guide, a photo-blog documenting New York City's 90 historic districts. You can read about his past and present projects at

Follow @samuelclay on Twitter.

You can email Samuel at He loves receiving email from new people. Who doesn't?