2
votes

I got a function that looks like this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui-1.8.13.custom.min.js" type="text/javascript"></script>

function getValues (fieldName, action){
            $("#" + fieldName).keyup(function () {
                if (this.value != this.lastValue){
                    if (this.timer) clearTimeout(this.timer);
                    this.timer = setTimeout(function () {
                        //$( "#"+fieldName ).autocomplete({source:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val()});
                        $.ajax({
                            type: "POST",
                            dataType: 'json',
                            url:"http://www.expat-job.com/ajax/" + action + "/keyword/" + $("#" + fieldName).val(),
                            success:function(msg) {
                                //splitedmsg = msg.split(',');
                              $( "#"+fieldName ).autocomplete(msg);
                            }
                        });
                    }, 200);
                    this.lastValue = this.value;
                }
            });
        }

It is then called like this:

$('input').live('click', function() {

                var word = $(this).attr('id');
                var splitedWord = word.split('-');
                switch(splitedWord[1])
                {
                    case 'CompanyName':
                        getValues(word, 'cv-company');
                    case 'DegreeName':
                        getValues(word, 'degree-name');
                    case 'InstituteName':
                        getValues(word, 'institute-name');
                    case 'LanguageName':
                        getValues(word, 'language-name');
                    case 'CertificationName':
                        getValues(word, 'certification-name');
                    case 'SkillName':
                        getValues(word, 'skill-name');
                    case 'JobTitle':
                        getValues(word, 'job-title');
                }
            });

The ajax response looks like this:

["Mondial Assistance","Mondial Assistance Asia Pacific","Mondial Assistance Group","Mondial Assistance Mauritius","Mondial Assistance Thailand"]

It's an array wrapped in json_encode().

My problem lies in the autocomplete part:

 $( "#"+fieldName ).autocomplete(msg);

I have tried every way possible to input the data. I've echoed a string and split it to get an array.

I've used different syntax: $( "#"+fieldName ).autocomplete({source: msg});

I always get the same error message:

$("#" + fieldName).autocomplete is not a function
success()cv (line 453)
msg = "["Mondial Assistance","...l Assistance Thailand"]"
F()jquery.min.js (line 19)
F()jquery.min.js (line 19)
X = 0

After a lot of testing, I've found out that it works with a simple test like this:

$( "#"+fieldName ).autocomplete({source: ["orange","apple","pear"]});

So the problem is not that the function is missing or the library is not loaded or anything like that.

And now the question

Why?!

2

2 Answers

1
votes
<script src="http://code.jquery.com/jquery-1.5.2.min.js"></script> 

<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script> 

 $( "#"+fieldName ).autocomplete({source: msg} ); 

You are not setting the source.

0
votes

The way you're using the autocomplete widget is overly complicated -- the widget is actually supposed to simplify things for you. You DO NOT need to:

  1. Invoke this widget on keyup/click events
  2. Set timeouts
  3. Make AJAX calls

Here is how you use it:

$("input.requires-autocomplete").each(function () { // add "requires-autocomplete" class to appropriate inputs
  var action;
  switch ($(this).attr("id").split("-")[1]) {
  case "CompanyName":
    action = "cv-company";
    break; // you need to add breaks
  case "DegreeName":
    action = "degree-name";
    break;
  case "InstituteName":
    action = "institute-name";
    break;
  case "LanguageName":
    action = "language-name";
    break;
  case "CertificationName":
    action = "certification-name";
    break;
  case "SkillName":
    action = "skill-name";
    break;
  case "JobTitle":
    action = "job-title";
    break;
  }
  $(this).autocomplete({
    minLength: 2, // widget waits until 2 or more characters are entered
    delay: 500, //  widget waits until 500 ms past the last keystroke
    source: function (request, response) { // specifying a URL that returns JSON/JSONP was enough
                                           // but in that case the server-side script must expect a query string parameter
                                           // "term" (text inside the control) and "callback" (for JSONP requests)
                                           // not your case so we do it manually
                                           // we call your script via getJSON
                                           // pass the text inside the control in the format expected by your script
                                           // and call the response funciton passing in the JSON data
                                           // the last statement is short-circuited by passing response as the second
                                           // parameter to getJSON, which effectively calls the response function
                                           // and passes in the response JSON data
      $.getJSON("http://www.expat-job.com/ajax/" + action + "/keyword/" + request.term, response);
    }
  });
});

If you strip out the comments and the switch-case logic, the remaining code is roughly 5-6 lines.