Expression Engine Autocomplete with jQuery

394 Views Asked by At

I have the following code:

<script>
$(document).ready(function() {
  $(function() {
    var availableTags = [

    {model_ref}
    ];
  $( "#model" ).autocomplete({
  source: availableTags
  });
  });
 });

How can I get this to populate the input from expression engine entries?

2

There are 2 best solutions below

0
On

You can do what you're after with the expressionengine channel entries tag.

<script>
var availableTags = [
    {exp:channel:entries channel="yourchannel" limit="10" dynamic="no"}
    "{title}",
    {/exp:channel:entries}
];
</script>

This will produce some javascript like:

<script>
var availableTags = [
    "First value",
    "Second value",
    "Third value",
    ... etc ...
];
</script>

The only thing is this code will have to be in a template (not a standalone JS file).

0
On

@JamesNZ has it and this worked for me.

To avoid any weirdness with the way arrays work in javascript, I added an extra empty value to the end of the returned array, like this.

<script>
    var availableTags = [
        {exp:channel:entries channel="yourchannel" limit="10" dynamic="no"}
        "{title}",
        {/exp:channel:entries}
       "" //extra empty value here.
    ];
</script>