1

I'm trying to implement a auto complete feature that i found here on stack overflow jQuery UI AutoComplete: Only allow selected valued from suggested list

This is the jquery I'm using

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

This is my implementation

<div id="ServiceProvider">
<h3 style="background-color:#EA6A20 ;"><b>Please enter the name of the HR representative who provided service to you</b> </h3>
<input autocomplete="on" type="text" name="HRName"  id="HRName" value=""></br></br>


<script>
var validOptions = ["First Last", "First1 Last1", "First2 Last2"]
previousValue = "";

$('#HRName').autocomplete({
    autoFocus: true,
    source: validOptions
}).keyup(function() {
    var isValid = false;
    for (i in validOptions) {
        if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
            isValid = true;
        }
    }
    if (!isValid) {
        this.value = previousValue
    } else {
        previousValue = this.value;
    }
});

</script>
</div>

The error i get is

Uncaught TypeError: Object [object Object] has no method 'autocomplete' Customer_Survery.php:266
(anonymous function) Customer_Survery.php:266

Thanks for the help

Community
  • 1
  • 1
bassxzero
  • 4,164
  • 18
  • 29

2 Answers2

1

you need to include the library like this after loaded jquery library:

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

An advise is to wrap it inside document.ready()

<script>
$(document).ready(function(){
  var validOptions = ["First Last", "First1 Last1", "First2 Last2"]
  previousValue = "";

  $('#HRName').autocomplete({
    autoFocus: true,
    source: validOptions
  }).keyup(function() {
    var isValid = false;
    for (i in validOptions) {
        if (validOptions[i].toLowerCase().match(this.value.toLowerCase())) {
            isValid = true;
        }
    }
    if (!isValid) {
        this.value = previousValue
    } else {
        previousValue = this.value;
    }
  });
});

</script>
Alessandro Minoccheri
  • 32,975
  • 19
  • 110
  • 159
1

You need jquery UI that contains the autocomplete plugin:

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

You should also include one of the following css:

black-tie, blitzer, cupertino, dark-hive, dot-luv, eggplant, excite-bike, flick, hot-sneaks, humanity, le-frog, mint-choc, overcast,pepper-grinder, redmond, smoothness, south-street, start, sunny, swanky-purse, trontastic, ui-darkness, ui-lightness, and vader.

i.e.

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css">

Credit goes to Downloading jQuery UI CSS from Google's CDN

Community
  • 1
  • 1
Johannes Stadler
  • 573
  • 10
  • 19