empty text for Ajax.InPlaceEditor

I was playing with the Scriptaculous Ajax.InPlaceEdit control this afternoon, and realised it didn’t support having “empty” text (default text to show if there was nothing to edit).

Someone on the Wiki had a partial solution, but it’s a little out of date and wasn’t working for us 1.6-ers (Doesn’t use the cool new $super stuff).

So I wrote my own! Enjoy :)

Ajax.InPlaceEditorWithEmptyText = Class.create(Ajax.InPlaceEditor, {

  initialize : function($super, element, url, options) {

    if (!options.emptyText)        options.emptyText      = "click to edit...";
    if (!options.emptyClassName)   options.emptyClassName = "inplaceeditor-empty";

    $super(element, url, options);

    this.checkEmpty();
  },

  checkEmpty : function() {

    if (this.element.innerHTML.length == 0 && this.options.emptyText) {

      this.element.appendChild(
          new Element("span", { className : this.options.emptyClassName }).update(this.options.emptyText)
        );
    }

  },

  getText : function($super) {

    if (empty_span = this.element.select("." + this.options.emptyClassName).first()) {
      empty_span.remove();
    }

    return $super();

  },

  onComplete : function($super, transport) {

    this.checkEmpty();
    return $super(transport);

  }

});

what other people thought

Man, Javascript looks trashy with short lines…. copy + paste may be in order :)

Nik, June 24th, 2008 at 5:53 pm

i’ve got 1.8 and it doesn’t seem to work. it doesn’t break anything, but i get no “click to edit” showing up….. any ideas?

chadd, July 22nd, 2008 at 2:33 am

@chadd: Not sure :) Are you sure you’re creating a new InPlaceEditorWithEmptyText rather than just a new Ajax.InPlaceEditor?

Nik, July 22nd, 2008 at 9:55 am

that was it. thanks much. this works great now.

chadd, July 30th, 2008 at 1:30 am

hi
first,thanks for this extension of inplaceeditor
i’m with prototype 1.6.0.1 and everything works except if user press cancel or leave input box empty. In this case empty text don’t get back.
I change the last extension name ‘onComplete’ (which seems not to exists in my prototype version) by ‘leaveEditMode’. It works fine.
Result :
leaveEditMode : function($super, transport) {
this.checkEmpty();
return $super(transport);
}
May that help someone ;)

psyray, August 4th, 2008 at 11:18 am

Fantastic ! That did help.

albo, August 18th, 2008 at 4:13 am

I am getting a Ajax.InPLaceEditorWithEmptyText is not a constructor… What am I doing wrong? I am using the above code.

Grant Griffith, August 28th, 2008 at 6:45 am

I haven’t tried this code but this line looks suspect:

if (empty_span = this.element.select(”.” + this.options.emptyClassName).first())

Of course they will equal if you set empty_span to the conditional. Maybe a == would be more appropriate. Or maybe I’m missing something.

Keep it up!

Jan, September 8th, 2008 at 11:52 pm

@jan: Nope, that’s how it’s meant to be. It’s using the assignment operator there so that I can then use the element we’ve found inside the conditional block.

Nik, September 9th, 2008 at 10:32 am

Hi, i’m using it and everything looks nice until i tried to change some options like cancelText, savingText or clickToEditText…

Any Idea?

PS: Thanx so much for the extension any how… ;)

Karina, September 13th, 2008 at 2:53 am

@Karina: Hmmm, I’m not sure what’s wrong with that. Try emailing me your code and I’ll see what I can do :)

Nik, September 14th, 2008 at 7:45 pm

have your say

viagra online | buy viagra online | buy viagra order| buy viagra now | buy viagra pills | prices viagra | viagra mexico | viagra pharmacy | viagra price viagra online, order viagra Online, buy viagra order - online medical center (we take care of you)cialis online | buy cialis online | buy cialis order| buy cialis now | buy cialis pills | prices cialis | cialis mexico | cialis pharmacy | cialis price | cialis cialis online, buy cialis online, buy cialis order - online medical center (we take care of you)levitra online | buy levitra online | buy levitra order| buy levitra now | buy levitra pills | prices levitra | levitra mexico | levitra pharmacy | levitra price levitra online, buy levitra online, buy levitra order - online medical center (we take care of you)acomplia online | buy acomplia online | buy acomplia order| buy acomplia now | buy acomplia pills | prices acomplia | acomplia mexico acomplia online, buy acomplia online, buy acomplia order - online medical center (we take care of you)soma online | buy soma online | buy soma order| buy soma now | buy soma pills | prices soma | soma mexico | soma pharmacy | soma price soma online, buy soma online, buy soma order - online medical center (we take care of you)tramadol online | buy tramadol online | buy tramadol order| buy tramadol now | buy tramadol pills | prices tramadol | tramadol mexico tramadol online, buy tramadol online, buy tramadol order - online medical center (we take care of you)ultram online | ultram vpxl online | buy ultram order| buy ultram now | buy ultram pills | prices ultram | ultram mexico | ultram pharmacy ultram online, buy ultram online, buy ultram order - online medical center (we take care of you)vpxl online | buy vpxl online | buy vpxl order| buy vpxl now | buy vpxl pills | prices vpxl | vpxl mexico | vpxl pharmacy | vpxl price | vpxl shop vpxl online, buy vpxl online, buy vpxl order - online medical center (we take care of you)xenical online | xenical online | buy xenical order| buy xenical now | buy xenical pills | prices xenical | xenical mexico | xenical pharmacy xenical online, buy xenical online, buy xenical order - online medical center (we take care of you) sitemap.xml