Auto-suggest text field

More
5 years 7 months ago #68123 by waitz
waitz replied the topic: Auto-suggest text field
Hmmm, it wont really work, not even the auto-suggestion. I see that it says inputID, data and source:data while the original said states. I tried to change them to states instead of data, and then the auto-suggestion works again, but not that it is mandatory to choose from one of the items in the list...

Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |

Please Log in to join the conversation.

More
5 years 7 months ago - 5 years 7 months ago #68127 by tpartner
tpartner replied the topic: Auto-suggest text field
You should not need to change those. They are defined parameters of the function.

Here is a refined version that opens the selection list when the input is focused. The only thing you should need to modify is "answer68164X61X1025FAM06" for different input IDs.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		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 must select a value from the list.';
 
		mandatoryAutocomplete('#answer68164X61X1025FAM06', states);
 
		function mandatoryAutocomplete(inputID, data) {
 
			$(inputID).autocomplete({
				source: data,
				minLength: 0,
				change: function(event, ui) {
					var okay = 0;
					if($(inputID).val() != '') {
						$(data).each(function(){
							if($(inputID).val() == this) {
								okay = 1;
							}
						});
					}
					if(okay < 1) {
						alert (msg);
						$(inputID).val('');
					}
				}
 
			}).focus(function(){
				if (this.value == "") {
					$(this).autocomplete('search', '');
				}
			});
		}
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 5 years 7 months ago by tpartner.

Please Log in to join the conversation.

More
4 years 6 months ago #89945 by frederickjayne
frederickjayne replied the topic: Auto-suggest text field
I am trying to integrate the Expression Mananger Qcode.qid variable with the autocomplete scripts listed on this thread and on the workaround page, and am having trouble with it when using multiple text entry questions. For a single text entry question, this code works flawlessly:

<script type="text/javascript" charset="utf-8">

$(document).ready(function() {

var choices = "[some choices here delimited by commas]".split(',');

$('#question'+{Qcode.qid}+' input.text').autocomplete({
source: choices
});

});
</script>

where Qcode is replaced with the question code. However, when I use this same script with multiple text entry questions, it's a no go, unless I hard code the qid either in the selector string or by mimicking the approach taken in the workaround. Then, it works like a dream. Note that autocomplete won't work in multiple entry scenario by using the approach taken in the workaround unless the variable q1ID is initialized by the constant question id; trying to assign the value with the statement q1ID = {Qcode.qid} does not work.

Any suggestions?

Please Log in to join the conversation.

More
4 years 6 months ago #89946 by tpartner
tpartner replied the topic: Auto-suggest text field
Frederick, are you using LS version 2.0?

Are there any JavaScript errors?

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 6 months ago #89947 by tpartner
tpartner replied the topic: Auto-suggest text field
Okay, a little testing indicates that {Qcode.qid} does not work with multiple-short-text questions. You may want to file a bug report .

This, however, should work if you put it in the source of the multiple-short-text question:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var choices = 'one,two,three,four,five,six'.split(',');
 
		$('#question'+{QID}+' input.text').autocomplete({
			source: choices
		});
 
	});
</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 6 months ago #89948 by frederickjayne
frederickjayne replied the topic: Auto-suggest text field
Thanks very much for the rapid response. To answer your previous questions, I am on LS 1.92, not 2.0 (haven't had the courage yet to upgrade since I'll be likely doing it manually—but I will soon as I know it's essential). I don't see any sign of JS errors, just no functionality.

Regarding your suggested code, I am not sure what {QID} is. Is there an EM expression this corresponds to this or do you mean the literal QID value (e.g. if the qid is 925, I have noted that the selector '#question925 input.text' works perfectly)? My problem is that I need to be able to export and reimport surveys, and when I do so the IDs all change, which is why it would be sweet to just drop in Qcode.qid.

Thanks again for the help. I'll submit a bug report as suggested.

--FJ

Please Log in to join the conversation.

More
4 years 6 months ago #89960 by tpartner
tpartner replied the topic: Auto-suggest text field

I am on LS 1.92, not 2.0

I tested the script above in 1.92 and 2.0.

I am not sure what {QID} is.

{QID} is an EM variable that will return the ID of the question it is found in (925 in your case?). Using this will make the survey portable. In fact, to test I exported/imported the question from 2.0 to 1.92.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: frederickjayne

Please Log in to join the conversation.

More
4 years 6 months ago - 4 years 6 months ago #89989 by frederickjayne
frederickjayne replied the topic: Auto-suggest text field
It's all good now, thanks very much. BTW, on the bug tracker, the suggestion was made to use {self.qid} or {that.Qcode.qid} as well as {QID}. All seem to do the trick and it appears that {Qcode.qid} cannot work in this context.

Many thanks again.

—FJ

Addendum: Emboldened by this success, I have tried creating a MST question (just two subquestions) with different choice sets for each subquestion. Haven't succeeded yet; the choices for the first SQ don't appear; I get the choices for the second SQ for both of them.
Last Edit: 4 years 6 months ago by frederickjayne.

Please Log in to join the conversation.

More
4 years 5 days ago #97154 by arielmosto
arielmosto replied the topic: Auto-suggest text field
Hi Tony:

It would be possible to add in this code, validation also that responses can not be repeated?

For example, Alabama can select only once. If you re-selected, get the message "You can not select the same state twice."

Thank you very much, Ariel

Please Log in to join the conversation.

More
4 years 5 days ago - 4 years 5 days ago #97158 by arielmosto
arielmosto replied the topic: Auto-suggest text field
Hi Tony:

This is the code that I mentioned. It is possible to validate duplicate answers? (Not allowing) Thanks!

tpartner wrote: You should not need to change those. They are defined parameters of the function.

Here is a refined version that opens the selection list when the input is focused. The only thing you should need to modify is "answer68164X61X1025FAM06" for different input IDs.

<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		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 must select a value from the list.';
 
		mandatoryAutocomplete('#answer68164X61X1025FAM06', states);
 
		function mandatoryAutocomplete(inputID, data) {
 
			$(inputID).autocomplete({
				source: data,
				minLength: 0,
				change: function(event, ui) {
					var okay = 0;
					if($(inputID).val() != '') {
						$(data).each(function(){
							if($(inputID).val() == this) {
								okay = 1;
							}
						});
					}
					if(okay < 1) {
						alert (msg);
						$(inputID).val('');
					}
				}
 
			}).focus(function(){
				if (this.value == "") {
					$(this).autocomplete('search', '');
				}
			});
		}
	});
</script>

Last Edit: 4 years 5 days ago by arielmosto.

Please Log in to join the conversation.

More
4 years 5 days ago #97161 by tpartner
tpartner replied the topic: Auto-suggest text field
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.

Please Log in to join the conversation.

More
4 years 5 days ago #97163 by arielmosto
arielmosto replied the topic: Auto-suggest text field
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 !

Please Log in to join the conversation.

More
4 years 5 days ago #97168 by tpartner
tpartner replied the topic: Auto-suggest text field
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.
Attachments:

Please Log in to join the conversation.

More
4 years 4 days ago #97175 by arielmosto
arielmosto replied the topic: Auto-suggest text field
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>

Please Log in to join the conversation.

More
4 years 4 days ago - 4 years 4 days ago #97193 by tpartner
tpartner replied the topic: Auto-suggest text field
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.
Last Edit: 4 years 4 days ago by tpartner.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now