jquery ui - Autocomplete in dynamically added textbox -
how add auto-complete in dynamically added textbox? have used way $('#se').autocomplete();, not getting that.
$(document).ready(function() { var counter = 2; $("#addbutton").click(function () { var newtextboxdiv = $(document.createelement('div')) .attr("id", 'textboxdiv' + counter); newtextboxdiv.after().html( '<input type="text" placeholder="role" name="role' + counter + '" id="textbox' + counter + '" value="" > <input type="text" placeholder="search" name="search' + counter + '" id="se" value="" > <input type="hidden" name="search' + counter + '" id="se' + counter + '" value="" >'); newtextboxdiv.appendto("#textboxesgroup"); $('#se').autocomplete(); counter++; }); <div id='textboxesgroup'> <div id="textboxdiv1" class="form-inline control-group"> <%= text_field_tag('roles', nil,:id => 'textbox1')%> <%= text_field_tag('search', nil,:id => 'se')%> <%=hidden_field_tag(:id_search, value = "")%> <input type='button' value='add' id='addbutton'> </div> </div>
there issues string concatination , selection in code:-
main thing $('#se' + counter).autocomplete({source: availabletags});
weren't attaching counter here. , there no source too. in example have attached hummy source, static source or ajax inyour case.
see doc more details...
try this:-
$("#addbutton").click(function () { var newtextboxdiv = $(document.createelement('div')) .attr("id", 'textboxdiv' + counter); newtextboxdiv.after().html('<input type="text" placeholder="role" name="role' + counter + '" id="textbox' + counter + '" value=""> <input type="text" placeholder="search" name="search' + counter + '" id="se' + counter + '" > <input type="hidden" name="search' + counter + '" value="" >'); newtextboxdiv.appendto("#textboxesgroup"); $('#se' + counter).autocomplete({source: availabletags}); counter++; });
for more clarity should use jquery element constructor , build lements more readeability demo
var roleinput = $('<input/>',{ type:'text', placeholder:'role', name:'role'+counter, id:'textbox' + counter }); var searchinput = $('<input/>',{ type:'text', placeholder:'search', name:'search'+counter, id:'se' + counter }); var hidd=$('<input/>',{ type:'hidden', name:'searchhid'+counter, id:'searchhid' + counter }); newtextboxdiv.append(roleinput).append(searchinput).append(hidd); newtextboxdiv.appendto("#textboxesgroup"); $('#se' + counter).autocomplete({ source: availabletags }); counter++;
Comments
Post a Comment