Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Auto-suggest text field

  • waitz
  • waitz's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
12 years 4 months ago #68123 by waitz
Replied by waitz on 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 2.73.1+171220
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
12 years 4 months ago - 12 years 4 months ago #68127 by tpartner
Replied by tpartner on 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.
Code:
<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: 12 years 4 months ago by tpartner.
The topic has been locked.
More
11 years 3 months ago #89945 by frederickjayne
Replied by frederickjayne on 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?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago #89946 by tpartner
Replied by tpartner on 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.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago #89947 by tpartner
Replied by tpartner on 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:
Code:
<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 topic has been locked.
More
11 years 3 months ago #89948 by frederickjayne
Replied by frederickjayne on 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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago #89960 by tpartner
Replied by tpartner on 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
The topic has been locked.
More
11 years 3 months ago - 11 years 3 months ago #89989 by frederickjayne
Replied by frederickjayne on 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: 11 years 3 months ago by frederickjayne.
The topic has been locked.
More
10 years 8 months ago #97154 by arielmosto
Replied by arielmosto on 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
The topic has been locked.
More
10 years 8 months ago - 10 years 8 months ago #97158 by arielmosto
Replied by arielmosto on 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.

Code:
<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: 10 years 8 months ago by arielmosto.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 8 months ago #97161 by tpartner
Replied by tpartner on 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.
The topic has been locked.
More
10 years 8 months ago #97163 by arielmosto
Replied by arielmosto on 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 !
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose