Welcome, Guest
Username: Password: Remember me

TOPIC: Auto-suggest text field

Re: Auto-suggest text field 1 year 1 month ago #97161

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 740
  • Karma: 341
How many inputs and what question type(s) are you dealing with?

Can you attache a sample survey?
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97163

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Thanks Tony:

10 inputs and the question type its Multiple short text.

Here its an example working with autocomplete:

tony.cio.com.ar/19/index.php?sid=21915&lang=en

Thanks !
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97168

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 740
  • Karma: 341
If you are only concerned about non-duplicate answers and do not need to force selection from the autocomplete list, you can add something like this to the question source. (there is no need to add it to every sub-question)
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the question
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
 
		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 msg = 'You cannot have duplicate entries.';
 
		$('input.text', q1).autocomplete({
			source: states,
			minLength: 0,
			change: function(event, ui) {
				var thisInput = $(this);
				if($(thisInput).val() != '') {
					$('input.text', q1).not($(thisInput)).each(function(){
						if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
							alert (msg);
							$(thisInput).val('').focus();
							$('input.text', q1).autocomplete('close');
							return false;
						}
					});
				}
			},
			select: function(event, ui) {
				var thisInput = $(this);
				var okay = 1;
				$('input.text', q1).not($(thisInput)).each(function(){
					if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
						okay = 0;
					}
				});
				if(okay == 0) {
					alert (msg);
					return false;
				}
			} 			
		}).focus(function(){				
			if (this.value == "") {
				$(this).autocomplete('search', '');				
			}			
		});		
	});
</script>


Here is a sample survey:

Attachment limesurvey_survey_5956741.lss not found

Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97175

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Hi Tony, Works perfect with non-duplicate answers.

I need to use a list in csv. I tried to adapt the code, following workarounds, but I could not make it work. I generated the file .php and .csv exactly as workarounds.

Below I put the code in question I tried to adapt. thanks

<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the question
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
 
		 var url = "templates/copia_de_default/lista.php";
 
		var msg = 'You cannot have duplicate entries.';
 
var dataArr = [];
 
       $.getJSON(url,function(data){
 
           $.each(data,function(i, item){
 
               dataArr.push(item);
 
           });
 
		$('input.text', q1).autocomplete({
			source: dataArr,
			minLength: 0,
			change: function(event, ui) {
				var thisInput = $(this);
				if($(thisInput).val() != '') {
					$('input.text', q1).not($(thisInput)).each(function(){
						if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
							alert (msg);
							$(thisInput).val('').focus();
							$('input.text', q1).autocomplete('close');
							return false;
						}
					});
				}
			},
			select: function(event, ui) {
				var thisInput = $(this);
				var okay = 1;
				$('input.text', q1).not($(thisInput)).each(function(){
					if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
						okay = 0;
					}
				});
				if(okay == 0) {
					alert (msg);
					return false;
				}
			} 			
		}).focus(function(){				
			if (this.value == "") {
				$(this).autocomplete('search', '');				
			}			
		});		
	});
</script>
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97193

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 740
  • Karma: 341
The path to the PHP file doesn't look correct. Shouldn't it be this?
var url = "upload/templates/copia_de_default/lista.php";


Or, if your code is in the question source, you can use this:
var url = "{TEMPLATEURL}lista.php";
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 1 year 1 month ago by tpartner.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97203

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Tony: Still not working.

I'm using version 2.0

The workarounds seems to be for 1.9+

The code in the ,php file, is contained in workarounds its the following:
<?php
 
   $countriesArr = array();
 
   $file_handle = fopen("lista.csv", "r");
 
   while (!feof($file_handle) ) {
 
       $line_of_text = fgetcsv($file_handle);
 
       array_push($countriesArr, $line_of_text[0]);
 
   }
 
   fclose($file_handle);
 
   echo json_encode($countriesArr);
 
?>

Thanks !
Last Edit: 1 year 1 month ago by arielmosto. Reason: forgot change something
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97210

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 740
  • Karma: 341
Okay, here's another approach using the jquery-csv plugin.

1) Download jquery.csv.js and place it in your template directory.

2) Place your CSV file in your template directory

3) Add this to startpage.pstpl AFTER the {TEMPLATEJS} placeholder:
<script type="text/javascript" src="{TEMPLATEURL}jquery.csv.js"></script>

4) Add a script like this to the question source:
<script type="text/javascript" charset="utf-8">	
	$(document).ready(function() {
 
		// Identify the question
		var q1ID = '{QID}';
		var q1 = $('#question'+q1ID+'');
 
		var url = '{TEMPLATEURL}countries.csv';
		var countries = new Array();
 
		// Grab the CSV contents
		$.get(url, function(data) {
 
			// Convert CSV contents to an array of arrays
			tmp = $.csv.toArrays(data);
 
			// Load the countries array
			$(tmp).each(function(i, item) {
				countries.push(item[0]);
			});
 
			var msg = 'You cannot have duplicate entries.';
 
			$('input.text', q1).autocomplete({
				source: countries,
				minLength: 0,
				change: function(event, ui) {
					var thisInput = $(this);
					if($(thisInput).val() != '') {
						$('input.text', q1).not($(thisInput)).each(function(){
							if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
								alert (msg);
								$(thisInput).val('').focus();
								$('input.text', q1).autocomplete('close');
								return false;
							}
						});
					}
				},
				select: function(event, ui) {
					var thisInput = $(this);
					var okay = 1;
					$('input.text', q1).not($(thisInput)).each(function(){
						if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
							okay = 0;
						}
					});
					if(okay == 0) {
						alert (msg);
						return false;
					}
				} 			
			});	
		});	
	});
</script>



Here is a template and working demo survey (install the template first).

In this survey a list of countries is imported from a CSV file for the autocomplete function. I disabled immediate pop-up of the autocomplete as it's very long so you need to type at least one letter to fire it.

Attachment test_csv_2.zip not found


Attachment limesurvey_survey_595674.lss not found

Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 1 year 1 month ago by tpartner.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97238

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Thanks Tony!

The last thing I ask.

I need the warning/alert if not selected from the list.

For Example: "If your option is in the list, please use the mouse to select it. If not, write it."

I'm not forced to choose from the list. But I suggest using it in cases as possible.

Thank you very much.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97264

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4054
  • Thank you received: 740
  • Karma: 341
I would be inclined to put those instructions in the question or help text so you don't have too many pop-up alerts.
Cheers,
Tony Partner

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

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 1 month ago #97267

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
Dear Tony:

The survey will use this question, we have been doing every year since 2003. We're migrating from another old app.

One problem we have is that people do not use the mouse to select the right choice, they write their answers. This creates a headache later, editing the basis of results, many answers are confusing.

The pop-up alerts would be extremely useful.

Thanks! Ariel
Last Edit: 1 year 1 month ago by arielmosto. Reason: error english
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.222 seconds
Donation Image