Welcome, Guest
Username: Password: Remember me

TOPIC: Auto-suggest text field

Re: Auto-suggest text field 2 years 11 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.

Re: Auto-suggest text field 2 years 11 months ago #68127

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

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

Re: Auto-suggest text field 1 year 10 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.

Re: Auto-suggest text field 1 year 10 months ago #89946

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

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 10 months ago #89947

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

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 10 months ago #89948

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

Re: Auto-suggest text field 1 year 10 months ago #89960

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: frederickjayne

Re: Auto-suggest text field 1 year 10 months ago #89989

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: 1 year 10 months ago by frederickjayne.
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 4 months ago #97154

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
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
The administrator has disabled public write access.

Re: Auto-suggest text field 1 year 4 months ago #97158

  • arielmosto
  • arielmosto's Avatar
  • OFFLINE
  • Junior Lime
  • Posts: 29
  • Karma: 0
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: 1 year 4 months ago by arielmosto.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.205 seconds
Donation Image