I am trying to write google like search (instant/Auto search) that shows values from database depending on the characters typed in input field. The database results are displaying while typing the chars. However, if I select any option from list of available list options, then the selected value is not updating into textbox. Any help will be appreciated.
There is nothing in this code that would cause the behaviour you mention. Added: Oops, missed your fill method! You need to add a, perhaps, click event to the listed suggestions that then grabs the text and sets this as the value of your input.
What are the suggestions - are they a ul/li list or select/options? In either case it is probably the click event that you could use.
Because your list of suggestions are dynamically added to the HTML you need to use the jQuery live() method (or for jQuery 1.7, on()). You can't just use click() directly, as these elements won't exist on page-load. Something like:
Code:
$('.suggestions').live('click', function () {
$('#inputString').val($(this).val()); // or ($(this).text());
});
which assumes your (new) suggestions each have the class "suggestions".
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 08-11-2012 at 10:47 PM..
I have the events in below code. This code retreves the database values.
Code:
<%
String name=request.getParameter("queryString");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost", "user", "password");
Statement st=con.createStatement();
//Add the data into the database
String sql = "SELECT EMP_EMPLOYEE_ID, EMP_FNAME, EMP_LNAME FROM UAP.dbo.UAP_EMPLOYEE where EMP_FNAME LIKE '%"+name+"%' OR EMP_LNAME LIKE '%"+name+"%';";
Statement stm = con.createStatement();
stm.executeQuery(sql);
ResultSet rs= stm.getResultSet();
while (rs.next ()){
out.println("<li onclick='fill("+rs.getString("EMP_FNAME")+");'>"+rs.getString("EMP_FNAME")+"</i>");
}}catch(Exception e){
out.println("Exception is ;"+e);
}
%>
Of course, you could always investigate the jQuery autocomplete plug-in. You just need to feed your post data as the plug-in's, autocomplete-object's source. But, as you're almost there, there is probably no initial benefit.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Try removing onblur="fill();" from your input - the suggestion may be used, but this code may immediately remove the text.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
Last edited by AndrewGSW; 08-11-2012 at 10:55 PM..
Thanks for your speedy reply. Is there any code change that you recommend in above files instead of using the plug-in that you suggested. Also I am getting the Values from DB and not hardcoded as shown in example.
Last edited by naveendk.55; 08-11-2012 at 10:57 PM..
Try removing onblur="fill();" from your input - the suggestion may be used, but this code may immediately remove the text.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS