Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Dynamically adding a text field, with autocomplete

We all love auto complete. It makes our lives a little less complicated and classy. I decided to (as a complimantry to my new JS beloved framework) to use the JQuery autocomplete plugin, problems began when I needed to use dynamically added text fields with autocomplete. Since the plugin assumes that you use a single and unique DOM ID, was unable to setup the autocomplete feature to newly dynamic fields.
One solution was to add the fields by javascript and Dom manipulation, but I prefered to use a partial in order to keep the code simple and Rubish. The other solution was to add a javascript script tag in the end of the partial which traverses the DOM and adds a unique and random id, and of course setting the auto complete feature for each one of them.
Bottom line, it is working and even working well, but I would like to hear new ideas if anyone has.
Over and out.

4 comments:

  Toby M

January 29, 2009 at 10:12 AM

I'm about to attempt the same type of thing. Care to share the code you used?

  Elad Meidar

January 29, 2009 at 10:24 AM

Sorry, but the project was delieverd a while ago and i can't access the code anymore.
from what i remember it's pretty simple.
i created a js function that runs right after the dom insert of the new field is being added,
This function traverses the DOM and identifies all the fields from the kind that we added (class="autocomplete_field" for example) and enables the JQuery autocomplete for each of them.
got it?

  Toby M

January 29, 2009 at 10:27 AM

Yes; this is precisely what I was going to try. Glad to hear you were successful with it (and that it was easy :)

  Elad Meidar

January 29, 2009 at 10:35 AM

Great,
don't forget to come back and post a link to your own post.


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels