My requirement is to show few options when user input some characters (minimum 3) in one of input field which might be added dynamically too.
I can not load data at page loading at beginning because data is huge. There is an ajax call to get that filtered data.
The issue what I am getting is Expected identifier
error on page loading at line# 2. So, could you please tell what is wrong with the below code?
$(document).on('keydown.autoplete', 'input.searchInput', function() {
source: function (request, response) { // Line # 2
var id = this.element[0].id;
var val = $("#"+id).val();
$.ajax({
type : 'Get',
url: 'getNames.html?name=' + val,
success: function(data) {
var id = $(this).attr('id');
$(this).removeClass('ui-autoplete-loading');
response(data);
},error: function(data) {
$('#'+id).removeClass('ui-autoplete-loading');
}
});
},
minLength: 3
});
My requirement is to show few options when user input some characters (minimum 3) in one of input field which might be added dynamically too.
I can not load data at page loading at beginning because data is huge. There is an ajax call to get that filtered data.
The issue what I am getting is Expected identifier
error on page loading at line# 2. So, could you please tell what is wrong with the below code?
$(document).on('keydown.autoplete', 'input.searchInput', function() {
source: function (request, response) { // Line # 2
var id = this.element[0].id;
var val = $("#"+id).val();
$.ajax({
type : 'Get',
url: 'getNames.html?name=' + val,
success: function(data) {
var id = $(this).attr('id');
$(this).removeClass('ui-autoplete-loading');
response(data);
},error: function(data) {
$('#'+id).removeClass('ui-autoplete-loading');
}
});
},
minLength: 3
});
Share
Improve this question
edited Jul 19, 2017 at 15:00
Avag Sargsyan
2,5234 gold badges32 silver badges45 bronze badges
asked Oct 29, 2015 at 9:48
JaikratJaikrat
1,1544 gold badges25 silver badges49 bronze badges
0
3 Answers
Reset to default 5How about using another approach: initialize the autoplete when you create the input:
$(function() {
// settings for each autoplete
var autopleteOptions = {
minLength: 3,
source: function(request, response) {
$.ajax({
type: "GET",
url: "getNames.html",
data: { name: request.term },
success: function(data) {
response(data);
}
});
}
};
// dynamically create an input and initialize autoplete on it
function addInput() {
var $input = $("<input>", {
name: "search",
"class": "searchInput",
maxlength: "20"
});
$input
.appendTo("form#myForm")
.focus()
.autoplete(autopleteOptions);
};
// initialize autoplete on first input
$("input.searchInput").autoplete(autopleteOptions);
$("input#addButton").click(addInput);
});
<form id="myForm" name="myForm" method="post">
<input id="addButton" type="button" value="Add an input" />
<input name="search" class="searchInput" maxlength="20" />
</form>
jsFiddle with AJAX
The method where I am adding new input field there writing below code.
function addInput(){
// Code to append new input filed next to existing one.
$("table").find('input[id=clientId]:last').autoplete({
source: function (request, response) {
var id = this.element[0].id;
var val = $("#"+id).val();
$.ajax({
type : 'Get',
url: 'getName.html?name=' + val,
success: function(data) {
var id = $(this).attr('id');
$(this).removeClass('ui-autoplete-loading');
response(data);
},
error: function(data) {
$('#'+id).removeClass('ui-autoplete-loading');
}
});
},
minLength: 3
});
}
And some where in other js which will be used to all other (static) input fields below code is used.
jQuery("input.searchInput").autoplete({
source: function (request, response) {
var id = this.element[0].id;
var val = $("#"+id).val();
$.ajax({
type : 'Get',
url: 'getName.html?name=' + val,
success: function(data) {
var id = $(this).attr('id');
$(this).removeClass('ui-autoplete-loading');
response(data);
},
error: function(data) {
$('#'+id).removeClass('ui-autoplete-loading');
}
});
},
minLength: 3
});
Note :- For any autoplete requests in dynamically added input fields, AutoComplete code of addInput() function will be called.
Thanks to @Salman and this post Enabling jQuery Autoplete on dynamically created input fields to give me an idea.
Try this.
$("#autopleteElement").autoplete({
source:function (data, response) {
$ajax({
url:'your/url?name='+data.term,
success:function(data){
response(data);
}
})
}
});
This code based on jquery UI autoplete.