Pastlifeuknow Pastlifeuknow - 3 months ago 24x
Javascript Question

How to set Jquery Autocomplete to a specific value and display It's Label using a datasource of JSON objects


So I have a table that is populated by a form. Each row can be edited by hitting a edit button. The Edit button opens the form that is populated. I need to auto fill the autocomplete so that the user can see one of His selected course.

How I Cheated

I'm using PHP and Codeigniter server side and am dynamically creating my form based on database. The labels and values are all produced from the Database and populate my JQuery Auto complete (a.k.a datasource variable below). From my controller I'm passing my value to the model and getting the Label from the DB. From there I'm passing it to my view and to my AutoComplete and setting the input value equal to the found label.

I feel dirty having done it this way. The inefficiency of this burns my eyes.

My Goal

I want to use the value that I've gotten and have the autocomplete select it and display it's label client side.


I need to just display the label in the box so the user knows it's not a blank field.

Both options need to allow the user to modify the autocomplete box.

Existing Code

My code for the input looks like this:

<div class="row-start span-2">
<label for="course_code">Course Code </label>
<div class="row-end span-2">
<input id="course_code">

My script for the autocomplete looks like this:

function search_course_code(){
var datasource = [{"value":"1","label":"AAF100 - DL"},{"value":"2","label":"AAF101 - DL+web"},.....];
var searchboxid = "#course_code";
var searchresultid = "#CRSEINVID";
focus: function( event, ui ) {
$( searchboxid ).val( ui.item.label );
return false;
select: function(event,ui){
var UIvalue = ui.item.value;
var UIlabel = ui.item.label;
$( searchboxid ).val( ui.item.label );
use_search("#search1","#CRSEINVID",UIvalue,UIlabel ); return false;
function use_search(show_select,result_id,uivalue,uilabel){
//loads value to field that takes it's value
//Display course below search box
course = "<span>"+uilabel+"</span>";
$(show_select).html(course );

//stops the value from being shown in the search box
return false;

$( document ).ready(function() {

I draw the value from a hidden input with a unique ID simply using JQUERY val() function.

What I've tried

Try 1

Setting value using:
Result: Value displayed not the label

Try 2

Using the autocomplete on create method I tried to overwrite the UI object and send it to the select.


Result: No observable change, no errors.

Try 3

$(searchboxid).autocomplete("select", hiddenInputValue);


Uncaught Error: cannot call methods on autocomplete prior to
initialization; attempted to call method 'select'

Try 4

Tried changing value using


and having change function detect it and set label with

$( searchboxid ).val( ui.item.label );

Result: Value loaded into input not label

Try 5

Tried Triggering the change function with this:

$("#<?php echo $id;?>").autocomplete("option","change").call(searchBox);

and then setting label. Based on the answer to:

jQuery AutoComplete Trigger Change Event

Result: empty UI object for change function,

Try 6

Tried Triggering the select function with this:

$("#<?php echo $id;?>").autocomplete("option","select",{value:hiddenInputValue}).call(searchBox);

and then using my current select function.

Result: Uncaught Error: undefined is not a function,


Ideas 1:

I thought of using the value then searching through the datasource object to find associating label and then using:


would this work? How would I do it?

Idea 2:

If the value of the input field is set to a value using:


Would the change function detect it?
Not detected used console.log function in change function to give feedback,


So after much research and trying to get this to work I discovered two problems:

  1. that I was using Select2 version 3.5.3 and needed to use text instead of label and :


  1. The MAJOR source of my problem though was because I was using Web Experience Toolkit tabs and I needed to load the Select 2 after tabs where initialized.