views:

1524

answers:

4

hi,

i am new to jquery, i am working on a web page which needs generating text boxes dynamically with autocomplete facility.

i tested $("#some").autocomplete(data); on some static content, it worked perfectly.

however, when i try the same technique with dynamically generated text boxes it's not working!

my code looks as follows:

$(function() {  

  $("#button_newproduct").click(function(){  
    $("#products_table > tbody").append(
      "<tr><td><input type='text'name='td_products["+counter+"]'/></td></tr>");
  });
  var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" ");
  $('input[name^=td_products]').autocomplete(data);
});

thanks guys i am done with this with ur help.

now, another problem. i am loading the array(input to autocomplete) with a DWR call.as below

DwrService.populateProducts(someFunc);
function someFunc(result){
    autoProducts=result;
    input.autocomplete(result);
 }

here problem is everytime making a DWR call to DB to get the array!

is there any way to store the array from DWR in a global variable?

regards

+1  A: 

Some thoughts:

  • Where is counter initialized?
  • Try leaving a space between the 'type' and 'name' attributes of your input tags, <input type='text' name='td_products... >, that might be preventing your startsWith attribute filter from matching anything.
karim79
thanks for the repla, i have just given some sample code but all those things are perfectly placed in my code
sangram
@sangram - I think @Kazar has spotted the main problem, see his answer.
karim79
+4  A: 

The main issue i think is that you are calling the autocomplete outside of the click handler. So the autocompletion gets set up when the page loads, rather than when the button is clicked.

To resolve this, alter the code to read:

$(function() {

    $("#button_newproduct").click(function() {

        var newItem = $("<tr><td><input type='text'name='td_products["+counter+"]'/></td></tr>");

        $("#products_table > tbody").append(newItem); 

        var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" "); 
        newItem.find('input').autocomplete(data);

    });
});

Now the autocompletion is being set on each new item, rather than once, at the start.

Kazar
I'm not certain that this is the _entire_ issue (karim79's point about whitespace could be important as well), but this is definitely a _key_ issue. The element that needs autocomplete doesn't exist when `autocomplete` is called since no click has happened to create it.
James A. Rosen
Surely if the input element does get added to the page, then the whitespace isn't an issue?
Kazar
awesome, worked perfectly i amazed at the speed of the reply even i am new to stack overflow thank you very much.
sangram
thanks, all of u guys i just posted some sample code all those elements are placed well in actual code.
sangram
If it worked, any chance of marking it as accepted?
Kazar
A: 

You're adding a new input when you click #button_newproduct but you're only adding the autocomplete once in the function, outside the click.

Check jquery live

Steerpike
Live won't work with autocomplete, it only works with events.
Kazar
Ah, of course, duh me, for some reason I got all caught up on the 'click' rather than the autocomplete.
Steerpike
+2  A: 

You need to add the autocomplete handler to each element as you add it. The elements that don't exist when you apply it on document load will never have it applied otherwise. Also, you'd be better off creating the row and input separately. By doing that you could just use the reference to the newly created input and use it with the autocomplete plugin.

$(function() {  

    $("#button_newproduct").click(function(){
         var input = $("<input type='text' name='td_products["+counter+"]' />");
         var row = $('<tr />').append( $('<td />').append(input) );  
         $("#products_table > tbody").append(row);
         var data = "Core celectors cttributes craversing canipulation CSS cvents cffects cjax ctilities".split(" ");
         input.autocomplete(data);

    });
});
tvanfosson
thank u, that worked.
sangram