I have few textbox in razor partial view which will be available based on condition. Out of that 1 is attached to autocomplete with below code but when the partial view is loaded then i get error (if textbox is not available)
0x800a138f - JavaScript runtime error: Unable to set property '_renderItem' of undefined or null reference
if the textbox is available in Razor view then error doesn't appear.
My code for Jquery is
<script type="text/javascript">
$(function () {
var view = $(document).findByClass("product-view");
var companysource = view.data("datasource-url");
$("#txtcompany").autocomplete({
minLength: 0,
source: function (request, resonse) {
$.ajax({
url: companysource,
data: { term: $('#txtcompany').val() },
dataType: "json",
type: "GET",
success: function (data) {
resonse(data);
}
});
},
focus: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
select: function (event, ui) {
$("#txtcompany").val(ui.item.Name);
return false;
},
change: function (event, ui) {
if (ui.item == null) {
} else {
}
}
})
.data("ui-autocomplete")._renderItem = function (ul, item) {
return $("<li>")
.data("ui-autocomplete-item", item)
.append("<div style='margin-bottom:2px; padding:1px 1px; font-size:14px;'><a>" + "<b>Company Name: </b>" + item.Name + "</a></div>")
.appendTo(ul);
};
});
You should first check if textbox is present in dom:
var txtcompany = $("#txtcompany");
alert(txtcompany.length);
if (txtcompany.length === 1) {
txtcompany.autocomplete({ ...
};
User contributions licensed under CC BY-SA 3.0