views:

33

answers:

2

Hello, this is my first post here, I've found answers here before I have a form where I can add fields that have autocomplete.
Instead of adding the autocomplete to each field as I create them, is it possible to use event binding to add the autocomplete?
I am a newbie when it comes to events in javascript.
This is what I have done to add a new line, using event binding (works perfectly).

$(function() {
    $('input[name=addItem]')
        .bind('click', function() {
            var line = $('#list > div').size()+1;
            divId = 'line-'+line;
            var divCloned = $('#line-1').clone();
            divCloned.attr('id', divId);
            $('#list').append(divCloned);
            var inputs = $(divCloned).children('input').get();
            for (var i in inputs) {
                inputs[i].value='';
                inputs[i].id = inputs[i].id.replace(/\d+/, line);
                inputs[i].name = inputs[i].name.replace(/\d+/, line);
            }
            this.blur();
        });
});  

I'm not even sure if I have presented my question in a proper way.
Thank you.

A: 

If I understand your question correctly, you want your autocomplete to be applied to all input fields? You can do that by generalizing your selector, so instead of adding the event to $('input[name=addItem]') (Meaning: To all tags of type "input" with an attribute "name" set to "additem") you can for example use $('input[type=text]') which will bind your event to all input-tags with attribute "type" set to "text".

Depending on your application, would you want to change the suggested auto-completes based on which input-field is being clicked on? That'd need more logic in your event handler.

Alexander Sagen
`$('input[type=text]')` will bind autocomplete to all appropriate `input` s that exist at the DOM when that method is fired. So when the OP dynamically adds a new `input` it will not have an autcomplete bound to it. -------- Note the second `input` in this example ==> http://jsfiddle.net/fbG5h/
Peter Ajtai
True, I interpreted the question to as all the DOM-elements were present when attached.
Alexander Sagen
Since the code includes `$('#list').append(divCloned);`, new elements are being added to the DOM dynamically.
Peter Ajtai
A: 

Option 1: Copy the original autocomplete

You can make a .clone() that includes event handlers using .clone(true):

var divCloned = $('#line-1').clone(true);

This should include the autocomplete for #line-1, so with the line above replacing the corresponding line in your code you should be all set.



Option 2: Create a new autocomplete with different options

If you want to change the autocomplete functionality, you can just set it again:

var divCloned = $('#line-1').clone(); // <== Don't copy the event handlers.

// ... do things, set variables, append divCloned, whatever

  // Now set the autocomplete with different options from the original
divCloned.autocomplete({ ... });
Peter Ajtai
Why would you want to make/clone n _equal_ event handlers for n inputs? Just use the same. And creating a new handler for each input is what he's trying to avoid in the first place.
Alexander Sagen
@sagen - You would have to use `live()` or `.delegate()` to add autocomplete to all now and future `input` s, and that's probably just as expensive.... `$("input").autcomplete(...)` would only add autocomplete for elements in the DOM at that point in time, then when you create a new `input` you have to somehow add handlers to it. ---- I haven't used the `.autocomplete()` plugin, so I'm not sure how it'd work with `.live()` or `.delegate()`.
Peter Ajtai
@Peter You only have to attach the existing event handler to your newly created elements, so var handler = func(e){}, div1.autoComplete(handler); div2.autoComplete(handler); has less overhead than cloning.
Alexander Sagen
@sagen - I'm not sure about that. It seems like they're very similar, since cloning with the `true` argument attaches the existing handler to the new element... which is what you are saying. Also, I don't quite get that code snippet.
Peter Ajtai