How to get jQuery autocomplete results to populate input and append data to div upon select?

Experts,

I need to update the following code to enable the following:

1) On select of an autocomplete option, fill the input with the selected "employee_name"
2) On select of an autocomplete option, append the "employee_name" to another div

The autocomplete function works properly in that it returns results made available via my external data source.

I am just unable to do steps 1 and 2 above.

Assistance would be appreciated.

Cheers!

$(function() {
	var projects = {};
	$( "#employee_name" ).autocomplete({
		minLength: 2,
		source: function( request, response ) {
			var term = request.term;
			if (term in projects) {
				response(projects[ term ] );
				return;
			}
			$.getJSON("./scripts/search.php?employee",request,function(data,status,xhr) {
				projects[term] = data;
				response(data);
			});
		},
		select: function(ui,item) {
			var employee_name = item.employee_name;
			$("#employee_results" ).empty();
			$("#employee_results").append("<b>Testing: </b>" + employee_name);
			return false;
		}
	})
	.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li>" )
		.append( "<a>" + item.employee_name + "<br>" + item.manager + "</a>" )
		.appendTo( ul );
	};
});

Open in new window


<table cellpadding="2" cellspacing="2" border="0" width="100%">
    <tr>
    <td width="15%" valign="top">
    <h3><label for="employee_name">Employee Name: </label></h3>
    </td>
    <td align="left" width="35%" valign="top">
    <div class="ui-widget">
    <input style="width:90%; height:25px; padding:2px;" name="employee_name" id="employee_name" >
    </div>
    </td>
    <td width="50%" valign="top" align="left">
    <div class="ui-widget">
        <div id="employee_results" style="overflow: auto;" class="ui-widget-content">
            <!-- THIS IS WHERE THE EMPLOYEE INFORMATION WILL APPEAR AS A RESULT OF SEARCH -->
            
        </div>
    </div>
    </td>
    </tr>
</table>

Open in new window

evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
what is the response for?
http://yourwebserver.adresse/scripts/search.php?employee?term=a
0
evibesmusicAuthor Commented:
@leakim971:

It is a jSON array.

When ?term=Har

[{"employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"},{"employee_name":"Smith Harris","employee_role":"SGT","facility":"FacilityB","department":"Receiving"}]
0
leakim971PluritechnicianCommented:
check the changes here :
			$(function() {
	var projects = {};
	$( "#employee_name" ).autocomplete({
		minLength: 2,
		source: function( request, response ) {
			var term = request.term;
			if (term in projects) {
				response(projects[ term ] );
				return;
			}
			$.getJSON("./scripts/search.php?employee",request,function(data,status,xhr) {
				projects[term] = data;
				response(data);
			});
		},
		select: function(ui,user_choice) {
			var employee_name = user_choice.employee_name;
			$("#employee_results").html("<b>Testing: </b>" + user_choice.item.employee_name);
			return false;
		}
	})
	.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li>" )
		.append( "<a>" + item.employee_name + "<br>" + item.employee_role + "</a>" )
		.appendTo( ul );
	};
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

leakim971PluritechnicianCommented:
basically we try : item.manager
but there's no " manager " field in your data, in bold what is available
employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"
0
evibesmusicAuthor Commented:
@leakim971:

Using your suggested code, I get the following error message:

TypeError: user_choice.item is undefined
$("#employee_results").html("<b>Testing: </b>" + user_choice.item.employee_name)
0
leakim971PluritechnicianCommented:
do e have the same line 16?
0
evibesmusicAuthor Commented:
@leakim971:

Yes, I do have exactly what you posted in line 16: select: function(ui,user_choice) {

I've read that there are some coding differences between jQuery versions.

I am using jQuery 1.11.1 & jQueryUI 1.11.0
0
leakim971PluritechnicianCommented:
any link to see that ?
0
evibesmusicAuthor Commented:
@leakin971:

Sorry but, the site is behind a firewall but, I have pulled the resulting source code so you can see what is being output.
0
leakim971PluritechnicianCommented:
yes and my code was working fine on my side with your data
0
evibesmusicAuthor Commented:
So the code I just provided works for you using the array example?

You have it posted where I can see it?
0
leakim971PluritechnicianCommented:
first stepClipboard04.png
0
evibesmusicAuthor Commented:
@Leakim971:

I see that it is working but, your example doesn't seem to be correct. You've typed in "ev" and you get both employees in the array but, neither of them have a consecutive "ev" in their names?

On my end the script is executing properly by only giving me the names of those employees who match what I have typed (in your example "ev").

?
0
leakim971PluritechnicianCommented:
are you trying to debug mine instead your?
as I don't have you php script, I'm just returning using php the data...
0
evibesmusicAuthor Commented:
@leakim971:

No, I am not trying to debug your code but, the image you posted seems to be giving you erroneous results based on the sample data I posted.

My problem is that when I select an option from the suggested fields, the value of the option I select is not placed into the input field. The selected option's value is also not placed into the div.

I am simply trying to figure out why? Would you mind posting your code so I can see what you have done? Maybe there is a slight difference which is causing my script not to work properly?
0
leakim971PluritechnicianCommented:
<?php
echo '[{"employee_name":"Bobo Harrington","employee_role":"MD","facility":"FacilityA","department":"Shipping"},{"employee_name":"Smith Harris","employee_role":"SGT","facility":"FacilityB","department":"Receiving"}]';
?>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.