Autocomplete + InPlaceEditor + Proto/Scriptaculous...finally!!

I blogged a while back about trying to mash up Spry/Prototype/Scriptaculous to do an inplace-edit and an autocomplete. Unfortunately, I lost a bit of momentum using Spry for this purpose because we're not using Spry in the office.

I picked up the ball to use just Prototype/Scriptaculous for our needs. What I found out was that there was a bit of a disconnect with Scriptaculous' Ajax.Autocompleter and Ajax.InPlaceEditor (to complete what I needed done).

What I needed: The ability to Add a Tag to a profile with InPlaceEditor but, I didn't want to have multiple versions of the same tag. (Ex. Dog, Dogs, Doggie) I needed to give the person who's inputting a new tag the ability to see tags that are similar to what he/she is currently typing.

Scriptaculous' Ajax.InPlaceEditor builds a form with an input or a textarea on the fly when you click on a link. Awesome! The only issue is that the

<input> or <textarea>
code doesn't include an ID for the html element created. The disconnect is that Autocomplete needs to target an INPUT's ID name.

With this knowledge, I had to make a change to Scriptaculous' library: contols.js. I am using the latest 1.8.0 version, so here is where I made my change at around line 551:


createEditField: function() {
...
fld.id = this.options.formId + '_input';
...
}
After making this change, viewing the generated javascript (god I love the web developer add-on for Firefox), I can see my form's dynamically generated input field, now has an ID. YAY!!!

Now I can continue with the Ajax.Autocompleter to target the INPUT field. But wait...There's a hangup :(

The generated form and the autocomplete execute almost simultaneously and there's an error. My Ajax.Autocompleter is complaining that the INPUT field that I am targetting doesn't exist? Huh? Doesn't exist? OH YEAH!! Need to do this in sequence :) My fix was to put a short setTimeOut function to execute the autocomplete library AFTER the INPUT field has been generated.

PHEW!! What a pain that was. But, hey, I learned alot with this little exercise. I'm almost certain that someone out there has done this before and probably did it better than me. If so, please point me in that direction :)

Here's the demo: Ajax.Autocompleter + Ajax.InPlaceEditor
(Try: Chris or phil or Boot or Hal)

Comments
Devon Burriss's Gravatar This is not meant to be a jQuery punt. And I'm not exactly sure what you were trying to accomplish as the demo link just took me back to the article but in jQuery adding a Dom element and attributes for that element is a cinch and then you have callback functions that execute on completion of another function...just putting out there how it would be done in jQuery. Prototype is a mature javascript library so I would think it would have something similar.
# Posted By Devon Burriss | 11/25/07 4:32 PM
Chris's Gravatar Sorry Devon. You're right. My URL was bad. I fixed it. And I'm sure jQuery works just as well, but unfortunately, I'm sorta locked into using Prototype/Scriptaculous. I'll take a look at jQuery when I have some free time.
# Posted By Chris | 11/25/07 4:35 PM
pedram's Gravatar this works ;)
# Posted By pedram | 12/15/07 2:36 AM
phramil's Gravatar Example you have is awesome, however I'm trying to get this to work but I can't seem to get the POST method to read the characters I'm actively entering in. It just seems to return everything in the query. Where you have your createTagsUL.cfm script , which variable is getting sent to that script (and is it via POST or GET)? I'm using PHP and I've traditionally used POST which worked with scriptaculous. My sample is very similar to what this person posted here: http://wiseguysonly.com/2006/04/14/ajax-autocomple...

Thanks!
# Posted By phramil | 1/4/08 5:17 PM
Chris's Gravatar @ phramil: The variable getting sent to the server is the input field.
new Ajax.Autocompleter('testID-inplaceeditor_input', 'autocomplete', "../action/createTagsUL.cfm", {paramName: "tag", parameters:"category=personal", minChars:2, indicator:'testID_indicator'});   }

variable sent to server: testID-inplaceeditor_input
receiving div element of server response: autocomplete
processing server URL: ../action/createTagsUL.cfm

Hope that answers your question :)
# Posted By Chris | 1/4/08 6:08 PM
phramil's Gravatar That's what I thought :P I've tried that but it seems like I must be overlooking something....

Thanks!
# Posted By phramil | 1/4/08 9:31 PM
phramil's Gravatar OK, sorry to bother you but I was wondering if you might be able to help me with this also. I've copied your code exactly as you have it and placed it on my server. I still cannot seem to get this to work. I've installed firebug into firefox and this is what I'm seeing that's happening.

When I type two or more letters, the page submits a POST to the script createTagsUL.cfm (I'm using PHP, but you are using coldfusion) which in turn queries a database for similar suggestions. According to firebug, the page is sending a POST of the letters I type, but the script is not seeing any POSTed variables. (or at least not the testID-inplaceeditor_input one).

At the end of my server-side PHP script, I put another <li></li> that echoes the POST'ed variable 'testID-inplaceeditor_input'. This way, when I type in any letters, it should post back what I'm typing - but it does not.

Am I missing something, or is it just late and I need to bang my head a few times on something really hard?
# Posted By phramil | 1/7/08 11:12 PM
phramil's Gravatar AH!! Nevermind - I figured out what was going on. I used print_r($_POST) to see what POSTED variables were being sent to the php server-side script, and it was $value, not 'testID-inplaceeditor_input'. I changed it accordingly and it works great!

Thanks for getting this to work so well... you should submit a change to Thomas Fuchs at script.aculo.us so he could implement this into the code permanently...

great job!
# Posted By phramil | 1/7/08 11:35 PM
Chris's Gravatar @phramil: thank you. I glad it works for you. Not sure that Mr. Fuchs will take my hack seriously. I'm sure the way I did it wasn't the most elegant. But, hey, it works. That's all I cared about :)
# Posted By Chris | 1/8/08 1:39 AM

Calendar

NAVIGATION

Recent Entries

Recent Comments

RSS

Search

Subscribe

Tags