Autocomplete not working

More
4 years 3 weeks ago #101191 by amazon3d
amazon3d created the topic: Autocomplete not working
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.

Please Log in to join the conversation.

More
4 years 3 weeks ago #101213 by tpartner
tpartner replied the topic: Autocomplete not working
QQ should be replaced by the question ID:


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

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:

Please Log in to join the conversation.

More
4 years 3 weeks ago #101215 by tpartner
tpartner replied the topic: Autocomplete not working
Or simpler, try this:
var q1ID = {QID};

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
4 years 3 weeks ago #101220 by amazon3d
amazon3d replied the 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?

Please Log in to join the conversation.

More
4 years 3 weeks ago #101221 by tpartner
tpartner replied the topic: Autocomplete not working
Something like this should work:

<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.

Please Log in to join the conversation.

More
4 years 3 weeks ago #101222 by amazon3d
amazon3d replied the 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?

Please Log in to join the conversation.

More
4 years 3 weeks ago #101223 by tpartner
tpartner replied the 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.

Please Log in to join the conversation.

More
4 years 3 weeks ago - 4 years 3 weeks ago #101228 by amazon3d
amazon3d replied the 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?
        $(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: 4 years 3 weeks ago by amazon3d.

Please Log in to join the conversation.

More
4 years 3 weeks ago - 4 years 3 weeks ago #101232 by tpartner
tpartner replied the topic: Autocomplete not working
Here's how I would have handled that:

<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: 4 years 3 weeks ago by tpartner.

Please Log in to join the conversation.

More
4 years 3 weeks ago - 4 years 3 weeks ago #101233 by amazon3d
amazon3d replied the topic: Autocomplete not working
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.


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:
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
<?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
Douglas
Broxton
Pearson
Hazlehurst
West Green
Ambrose
Atlanta
Orlando
Last Edit: 4 years 3 weeks ago by amazon3d.

Please Log in to join the conversation.

More
4 years 3 weeks ago #101260 by amazon3d
amazon3d replied the 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!

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!