Shane
Shane

Reputation: 697

JQuery autocompleter for dynamic input boxes

I have a webpage with jquery generating dynamic html input boxes.

Something like this appears on the page.

<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>

These text-boxes all use the same autocompleter, is it possible in jQuery to point my autocompleter at all of these?

Upvotes: 0

Views: 1334

Answers (2)

Mike
Mike

Reputation: 654

You could add a div that wraps input and that is never changed, then upon creation of new input store its id in jquery internal cache, just like this:

var $input = '<input name=somename[] type="text"/>';    
$('#mywrap').append($input);
$input.data('id', 'some id');

Then on you can access autocompleter in the following way:

$('#mywrap input').live('click', function() {
    var id = $(this).data('id');
    // and now do anything with the new id you have!
});

Upvotes: 0

RaYell
RaYell

Reputation: 70414

First of all id should be unique in the whole document so your code is not correct.

What you probably mean is

<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>

To enable autocomplete on those boxes just use the selector that will match them all

var data = "foo bar baz";
$('input[name^=numbers]').autocomplete(data);

Upvotes: 5

Related Questions