Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: Auto-suggest text field

Auto-suggest text field 4 years 9 months ago #67872

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
I have seen it somewhere in the forum, but I cannot find it.... :S

I had a script I could use in a sub-text field in a multiple short text question, that suggests answers based on what I start typing in the text field... The answers came from a list in the script of course.

I cannot find this back in the forum. Do you have an idea of a script like this?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67885

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67893

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
Rajan, once you have added all these nice workarounds, can you create a copy of that survey so we can have a look at a life version?

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67895

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
Sure :-) It is becoming quite advanced, nice for the user, and not so difficult to administer.
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67901

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
tpartner wrote: Hmmm... The text field I want to use this for is one of 5 subquestions in multiple short text.
<script type="text/javascript" charset="utf-8">
 
    $(document).ready(function() {
 
        var q1ID = #answer68164X61X1025FAM06;
 
        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(',');
 
        $('#question'+q1ID+' input.text').autocomplete({
			source: states		
		});
 
    });
</script>
I tried to put it in the code of the main question and in the code of the subquestion. Where does it actually go, and why won't it work... :huh:
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67903

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
You can place the script in the source of any question or subquestion. It just need to be present in the page.

To target a specific sub-question field, use something like this:
<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(',');
 
        $('#answer68164X61X1025FAM06').autocomplete({
			source: states		
		});
 
    });
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67905

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
Sure It is becoming quite advanced, nice for the user, and not so difficult to administer.

Soon the survey will complete itself :laugh:
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Auto-suggest text field 4 years 9 months ago #67918

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
tpartner wrote:
Soon the survey will complete itself :laugh:
Interesting thought :P
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
The administrator has disabled public write access.

Auto-suggest text field 4 years 8 months ago #68106

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
The script is working, but it is not mandatory that I choose one of the suggested words.
Is it possible to make this mandatory?
Version 1.91+ Build 11232 | PHP 5.3.9 | MySQL 5.1.56 |
Last Edit: 4 years 8 months ago by waitz.
The administrator has disabled public write access.

Auto-suggest text field 4 years 8 months ago #68112

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
This code will apply the autocomplete plugin to an input and if a value is entered that is not in the data array an alert will be shown and the input will be set to null.
<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		
			});
			$(inputID).change(function(){
			//$('#answer68164X61X1025FAM06').change(function(){
				var okay = 0;
				$(states).each(function(){
					if($(inputID).val() == this) {
						okay = 1;
					}
				});
				if(okay < 1) {
					alert (msg);
					$(inputID).val('');
				}
			});
		} 
 
	});
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The administrator has disabled public write access.

Auto-suggest text field 4 years 8 months ago #68123

  • waitz
  • waitz's Avatar
  • Offline
  • Gold Lime
  • Posts: 171
  • Thank you received: 1
  • Karma: 3
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 |
The administrator has disabled public write access.

Auto-suggest text field 4 years 8 months ago #68127

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
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: 4 years 8 months ago by tpartner.
The administrator has disabled public write access.

Auto-suggest text field 3 years 7 months ago #89945

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?
The administrator has disabled public write access.

Auto-suggest text field 3 years 7 months ago #89946

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
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.
The administrator has disabled public write access.

Auto-suggest text field 3 years 7 months ago #89947

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 6122
  • Thank you received: 1358
  • Karma: 534
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.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.300 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form