Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Autocomplete not working

  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #101191 by amazon3d
Autocomplete not working was created by amazon3d
I have been trying all day to get autocomplete to work with Lime Survey.
I tired something basic like states, cities, zip codes. I could not get any of them to work. I followed the instructions at Workarounds but was not able to get it working. Can someone point me to a more elaborate explanation of this guide? What am I supposed to replace QQ with? The question code, Input ID, Input Name? I've tried all 3 and autocomplete didn't fire off when I started typing. Example at remote.graycomputer.net/ (autocomplete test). Hosting it locally for testing.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101213 by tpartner
Replied by tpartner on topic Autocomplete not working
QQ should be replaced by the question ID:


So, in your test survey:
Code:
var q1ID = 114;

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101215 by tpartner
Replied by tpartner on topic Autocomplete not working
Or simpler, try this:
Code:
var q1ID = {QID};

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #101220 by amazon3d
Replied by amazon3d on topic Autocomplete not working
THANK YOU!!!!!!!!

I worked on this all day yesterday.
Second question is if you have multiple short answers how do you get this to work for something like an address (City, State, Zip) that have different lookups?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101221 by tpartner
Replied by tpartner on topic Autocomplete not working
Something like this should work:

Code:
<script charset="utf-8" type="text/javascript">
 
        $(document).ready(function() {
 
                var q1ID = {QID};
 
                var cities = "Greenville,Franklin,Clinton,Springfield,Bristol,Salem,Fairview,Washington,Madison,Georgetown,Arlington,Marion,Oxford".split(',');
 
                var states = "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware, District of Columbia,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky, Louisiana,Maine,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York, North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Maryland,Massachusetts,Michigan, Minnesota,Mississippi,Missouri,Pennsylvania,Rhode Island,South Carolina,South Dakota, Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming".split(',');
 
                var zips = "11111,22222,33333,44444,55555,66666,77777,88888,99999".split(',');
 
                $('#question'+q1ID+' input.text:eq(0)').autocomplete({
                        source: cities
                });
                $('#question'+q1ID+' input.text:eq(1)').autocomplete({
                        source: states
                });
                $('#question'+q1ID+' input.text:eq(2)').autocomplete({
                        source: zips
                });
 
        });
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #101222 by amazon3d
Replied by amazon3d on topic Autocomplete not working
input.text:eq(0)

Is this a built in feature so LS knows which part of the array your answering?
And will this also work with an array?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101223 by tpartner
Replied by tpartner on topic Autocomplete not working
That is a jQuery selector that finds the first input with a class of "text" - api.jquery.com/eq/

For arrays or other question types you may need to modify the selector. Use a tool like Firebug for Firefox to explore the DOM.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago - 10 years 4 months ago #101228 by amazon3d
Replied by amazon3d on topic Autocomplete not working
I tried modifying the code to use CSV files but it seems to hang Zip codes on the Cities and then breaks from there.
Did I do something wrong here?
Or did I go down the totally wrong path for CSV?
Code:
        $(document).ready(function() {
 
        var q1ID = {QID};
        var cities = "http://localhost/templates/default/cities.php";
  var dataArr = [];
  $.getJSON(url,function(data){
 
                        $.each(data,function(i, item){
                                dataArr.push(item);
                        });
 
                        $('#question'+q2ID+' input.text:eq(1)').autocomplete({
                                source: dataArr
                        });
 
                });
 
        });
 
        $(document).ready(function() {
 
                var q2ID = {QID};    
                var states = "http://localhost/templates/default/states.php";
    var dataArr = [];
    $.getJSON(url,function(data){
 
                        $.each(data,function(i, item){
                                dataArr.push(item);
                        });
 
                        $('#question'+q2ID+' input.text:eq(1)').autocomplete({
                                source: dataArr
                        });
 
                });
 
        });
 
        $(document).ready(function() {
 
                var q3ID = {QID};
                var url = "http://localhost/templates/default/zipcodes.php";
    var dataArr = [];
    $.getJSON(url,function(data){
 
                        $.each(data,function(i, item){
                                dataArr.push(item);
                        });
 
                        $('#question'+q3ID+' input.text:eq(2)').autocomplete({
                                source: dataArr
                        });
 
                });
 
        });
Last edit: 10 years 4 months ago by amazon3d.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago - 10 years 4 months ago #101232 by tpartner
Replied by tpartner on topic Autocomplete not working
Here's how I would have handled that:

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    var qID = {QID};
 
    var citiesURL = "http://localhost/templates/default/cities.php";
    var citiesArr = [];
    $.getJSON(citiesURL, function(data){
 
      $.each(data,function(i, item){
        citiesArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(0)').autocomplete({
        source: citiesArr
      });
    });
 
    var statesURL = "http://localhost/templates/default/states.php";
    var statesArr = [];
    $.getJSON(statesURL, function(data){
 
      $.each(data,function(i, item){
        statesArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(1)').autocomplete({
        source: statesArr
      });
    });
 
    var zipURL = "http://localhost/templates/default/zipcodes.php";
    var zipArr = [];
    $.getJSON(zipURL, function(data){
 
      $.each(data,function(i, item){
        zipArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(2)').autocomplete({
        source: zipArr
      });
    });
 
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 4 months ago by tpartner.
The topic has been locked.
  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago - 10 years 4 months ago #101233 by amazon3d
Replied by amazon3d on topic Autocomplete not working
[strike]Tried that iteration of the code and was not able to have success with it. I thank you for your time and patience Tony. I've attached all the code for the various parts of the script.
I'm going to continue trying to find my break point but if you see something obvious let me know.[/strike]

I was able to get Zip and City working but State is not firing, I checked the code in all 3 sections (CSV PHP and JS) and it appears to be identical aside from the variable names.

Question Source:
Code:
What is your address? 
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    var qID = {QID};
 
      var citiesURL = "http://localhost/templates/default/cities.php";
    var citiesArr = [];
    $.getJSON(citiesURL, function(data){
 
      $.each(data,function(i, item){
        citiesArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(0)').autocomplete({
        source: citiesArr
      });
    });
 
      var statesURL = "http://localhost/templates/default/states.php";
    var statesArr = [];
    $.getJSON(statesURL, function(data){
 
      $.each(data,function(i, item){
        statesArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(1)').autocomplete({
        source: statesArr
      });
    });
 
      var zipURL = "http://localhost/templates/default/zipcodes.php";
    var zipArr = [];
    $.getJSON(zipURL, function(data){
 
      $.each(data,function(i, item){
        zipArr.push(item);
      });
 
      $('#question'+qID+' input.text:eq(2)').autocomplete({
        source: zipArr
      });
    });
 
  });
</script>

cities.php source
Code:
<?php
 
        $citiesArr = array();
 
        $file_handle = fopen("http://localhost/templates/default/cities.csv", "r");
 
        while (!feof($file_handle) ) {
                $line_of_text = fgetcsv($file_handle);
                array_push($citiesArr, $line_of_text[0]);
        }
 
        fclose($file_handle);
 
        echo json_encode($citiesArr);
 
?>
cities.csv source
Code:
Douglas
Broxton
Pearson
Hazlehurst
West Green
Ambrose
Atlanta
Orlando
Last edit: 10 years 4 months ago by amazon3d.
The topic has been locked.
  • amazon3d
  • amazon3d's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
10 years 4 months ago #101260 by amazon3d
Replied by amazon3d on topic Autocomplete not working
I tried it this morning and apparently it just started working. The state field was not working yesterday evening but this morning it is so. Perhaps something got stuck and this is now resolved.
Thank you for your time and input Tony!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose