Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have coded for autocomplete to dynamically added textbox. it executes first row only. i add another row then autocomplete didn't works. please anyone help me..




Thanks & regards,
Vivek .R

What I have tried:

Script:
<script type="text/javascript">
$(document).ready(function () {
$(document).on("click", ".classAdd", function () { //on is used for getting click event for dynamically created buttons
var ic = $('#ItemCode');
var rc = $('#RateCode');
var iname = $('#ItemName');
//Add rows
var rowCount = ($('.data-contact-person').length + 1) - 1;
var contactdiv = '' +
'<input type="text" id="ItemName" name="ItemName" class="form-control" />' +
'<input type="text" id="IRateCode" name="[' + rowCount + '].IRateCode" class="form-control" />' +
'<input type="text" id="ItemCode" name="[' + rowCount + '].ItemCode" class="form-control " />' +
'<input type="text" id="QtyRequested" name="[' + rowCount + '].QtyRequested" class="form-control" />' +
'<button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>' +
'<button type="button" id="btnDelete" class="deleteContact btn btn btn-danger btn-xs">Remove</button>' +
'';
$('#maintable').append(contactdiv); // Adding these controls to Main table class
// alert(rowCount);
});

$(document).on("click", ".deleteContact", function () {
$(this).closest("tr").remove(); // closest used to remove the respective 'tr' in which I have my controls
});

//Autocomplete

$("#ItemName").autocomplete({
source: function (request, response) {
$.ajax({
url: "/MatReq/GetData?term=" + request.term,
type: "POST",
dataType: "json",
success: function (data) {
if (data.length > 0) {
response($.map(data, function (item) {
return { label: item.ItemName, value: item.ItemName
};
}))
}
else {
response([{ label: 'No results found.', val: -1}]);
}
}
})
},
select: function (event, ui) {
var itemname = ui.item.value;
if (ui.item.val == -1) {
ic.val('');
rc.val('');
iname.val('');
return false;
}
else {
$.getJSON('@Url.Action("GetDetail")', { itemname: ui.item.value }, function (data) {
if (data) {
$('#ItemCode').val(data.icode);
$('#RateCode').val(data.rcode);
}
});
}
},
// change: txtOneChanged,
messages: {
noResults: "", results: ""
}
});
</script>

View:
foreach (var i in Model)
{

@Html.TextBox("ItemName", null, new { id = "ItemName", @class = "form-control", autocomplete = "off" })
@Html.TextBoxFor(a =>a[j].IRateCode, new { id = "RateCode", @class = "form-control", autocomplete = "off" })
@Html.TextBoxFor(a => a[j].ItemCode, new { id = "ItemCode", @class = "form-control", autocomplete = "off" }) @Html.TextBoxFor(a => a[j].QtyRequested, new { id = "QtyRequested", @class = "form-control", autocomplete = "off" })

<button type="button" id="btnAdd" class="btn btn-xs btn-primary classAdd">Add More</button>

j++;
}
Posted
Updated 17-Aug-16 22:29pm

1 solution

You have two issues, when your page loads and the

$("#ItemName").autocomplete


runs, it attaches to all elements with an id of ItemName. If you create a new element with that id then it won't have autocomplete has you haven't told it to attach the autocomplete plug-in to it. Every time you create a new element you should also attach the autocomplete. The easiest way of doing this is to turn your autocomplete attach code into a function and you call that function on document load, and also when you add new items.

That still isn't going to work though :) as an id has to be unique on the page, you can only have one element with the id of ItemName, if you have three then only the first one is recognised. Rather than use ids you will need to use class names, or use "data" attributes to tag the relevant items instead.

Example;

Razor
<div id="target">
@foreach (var m in Model)
{
    <span>Auto</span>@Html.TextBox("ItemName", null, new {data_autocomplete="data-autocomplete"})<br/>
    <span>No auto</span>@Html.TextBox("TestBox")<br/>
}
</div>

<button onclick="return add();">Add</button>

<script type="text/javascript">
    $(document).ready(function () {
        setup();
    });

    function add() {
        var txt = $("<input type='text'></input>").attr("data-autocomplete", "data-autocomplete");
        $("#target").append(txt);
        setup();
        return false;
    }
    function setup() {
        $("[data-autocomplete]").autocomplete({ source: ["Apple", "April", "Blue", "Brown"] });
    }
</script>
 
Share this answer
 
v4
Comments
Vivek.anand34 18-Aug-16 6:00am    
Actually, $("#ItemName") its a 'Id' or 'name'.. Its Id means all Id in textbox same right.
F-ES Sitecore 18-Aug-16 6:07am    
No, "#ItemName" means an id of ItemName. "[name=ItemName]" means something with a name of ItemName.
Vivek.anand34 18-Aug-16 9:49am    
i put unique id ("ItemName[0],ItemName[1]...) but now 1st line also didn't work..
F-ES Sitecore 18-Aug-16 9:55am    
That's just going to end up a mess, don't use ids for this, use class names or data attributes.
Vivek.anand34 18-Aug-16 9:58am    
1st row is textbox like this @Html.TextBox("[0].ItemName", null, new { @class = "form-control", autocomplete = "off" })

from 2nd row like this: <input type="text" id="[' + rowCount + '].ItemName" name="[' + rowCount + '].ItemName" class="form-control" />

any prob in this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900