Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-11-2012, 10:17 PM   PM User | #1
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
Question AutoSuggest select not working

Hi,

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.

Code:
 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Auto Complete</title>
<script src="jquery/jquery.js" type="text/javascript"></script>
 
<script type="text/javascript">
function lookup(inputString) {
if(inputString.length == 0) {
$('#suggestions').hide();
} else {
$.post("states.jsp", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').show();
$('#autoSuggestionsList').html(data);
}
});
}
}
function fill(thisValue) {
$('#inputString').val(thisValue);
setTimeout("$('#suggestions').hide();", 200);
}
</script>
<style type="text/css">
body {
font-family: Helvetica;
font-size: 13px;
color: #000;
}
h3 {
margin: 0px;
padding: 0px;
}
.suggestionsBox {
position: relative;
left: 260px;
margin: 0px 0px 0px 0px;
width: 200px;
background-color: #7845DD;
-moz-border-radius: 7px;
-webkit-border-radius: 7px;
border: 2px solid #000;
color: #fff;
}
.suggestionList {
margin: 0px;
padding: 0px;
}
.suggestionList li {
margin: 0px 0px 3px 0px;
padding: 3px;
cursor: pointer;
}
.suggestionList li:hover {
background-color: #DD45CD;
}
</style>
</head>
<body>
<div>
<form>
<div> <h3><font color="red">Indian States</states></font></h3>
<br /> Enter India State Name to see autocomplete
<input type="text" size="30" value="" id="inputString"
onkeyup="lookup(this.value);" onblur="fill();" />
</div>
<div class="suggestionsBox" id="suggestions" style="display: none;">
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
</form>
</div>
</body>
</html>
naveendk.55 is offline   Reply With Quote
Old 08-11-2012, 10:39 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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..
AndrewGSW is offline   Reply With Quote
Old 08-11-2012, 10:43 PM   PM User | #3
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
Question

hi,

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);
}
%>
naveendk.55 is offline   Reply With Quote
Old 08-11-2012, 10:45 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 08-11-2012, 10:51 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Have you got a link to the page?

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..
AndrewGSW is offline   Reply With Quote
Old 08-11-2012, 10:55 PM   PM User | #6
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
Question

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..
naveendk.55 is offline   Reply With Quote
Old 08-11-2012, 10:59 PM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 08-11-2012, 11:08 PM   PM User | #8
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
Question

Nope, removing fill() didn't help.

Also Once I click on a value from drop down list, then I get an error as below.

"NASH is undefined".


In above line, NASH is a value that I clicked from the dropdown list.
naveendk.55 is offline   Reply With Quote
Old 08-11-2012, 11:32 PM   PM User | #9
naveendk.55
New Coder

 
Join Date: Aug 2011
Posts: 88
Thanks: 5
Thanked 0 Times in 0 Posts
naveendk.55 is an unknown quantity at this point
Question

I resolved it by changing the code in JSP file to:


Code:
click='fill(\""+rs.getString("EMP_FNAME")+"\");'>"+rs.getString("EMP_FNAME")+" </li>");

thanks for trying to help you.
naveendk.55 is offline   Reply With Quote
Old 08-11-2012, 11:34 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Code:
out.println("<li onclick='fill('"+rs.getString("EMP_FNAME")+"');'>"+rs.getString("EMP_FNAME")+"</i>");
There are a couple of extra apostrophes in the above code example.

Code:
fill(NASH)      // will look for something called NASH, which doesn't exist
fill('NASH')    // passes the string 'NASH' to the function.
or you might need this version:

Code:
out.println("<li onclick=\"fill('"+rs.getString("EMP_FNAME")+"');\">"+rs.getString("EMP_FNAME")+"</i>");
__________________
"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
AndrewGSW is offline   Reply With Quote
Old 08-11-2012, 11:39 PM   PM User | #11
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You got there just before (or just after..?) me
__________________
"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
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:15 AM.


Advertisement
Log in to turn off these ads.