Pre-fill a drop-down list with values from token table

More
3 weeks 3 hours ago #222166 by achecchini
Hi to all!
this week I tried to resolve the issue in the subject.

Referring to this old thread  on the forum I tried to use code suggested but, in my case, it seem not working. The drop-down list is about the national italian provinces (about 110 different items).

The code I try to use is this (only first occurence):

<script type="text/javascript" charset="utf-8">        
 
    $(document).ready(function() {
 
        if('{TOKEN:ATTRIBUTE_3}' == 'Agrigento' && $('#answer681778X1530X27648').val() == '') {
            $('#answer681778X1529X27648 option').removeAttr('selected');
            $('#answer681778X1529X27648 option[value="1"]').attr('selected', 'selected'); 
            $('#answer681778X1529X27648').val('1');
        }
        if('{TOKEN:ATTRIBUTE_3}' == 'Alessandria' && $('#answer681778X1530X27648').val() == '') {
            $('#answer681778X1529X27648 option').removeAttr('selected');
            $('#answer681778X1529X27648 option[value="2"]').attr('selected', 'selected'); 
            $('#answer681778X1529X27648').val('2');
        }

...

        if('{TOKEN:ATTRIBUTE_3}' == 'Vicenza' && $('#answer681778X1530X27648').val() == '') {
            $('#answer681778X1529X27648 option').removeAttr('selected');
            $('#answer681778X1529X27648 option[value="109"]').attr('selected', 'selected'); 
            $('#answer681778X1529X27648').val('109');
        }
        if('{TOKEN:ATTRIBUTE_3}' == 'Viterbo' && $('#answer681778X1530X27648').val() == '') {
            $('#answer681778X1529X27648 option').removeAttr('selected');
            $('#answer681778X1529X27648 option[value="110"]').attr('selected', 'selected'); 
            $('#answer681778X1529X27648').val('110');
        }
    });
</script>

Where 681778X1530X27648 is the SGQ code and the values "1", "2" etc etc are the answer code of the drop-down list.

After numerous tests I still don't understand why the code above doesn't work!

Has someone any I idea about my mistake or how to solve my issue??

Thank for help in advance.

AgoZ

 

Please Log in to join the conversation.

More
2 weeks 6 days ago #222193 by tpartner
I think something like this should work in the newer LS versions:

<script type="text/javascript" data-author="Tony Partner">	
	$(document).on('ready pjax:scriptcomplete',function(){
 
		var thisQuestion = $('#question{QID}');
		var thisSelect = $('select.form-control:eq(0)', thisQuestion);
 
		if('{TOKEN:ATTRIBUTE_3}' == 'Agrigento' && $(thisSelect).val() == '') {
			$(thisSelect).val('1').trigger('change');
		}
		if('{TOKEN:ATTRIBUTE_3}' == 'Alessandria' && $(thisSelect).val() == '') {
			$(thisSelect).val('2').trigger('change');
		}
 
...
 
		if('{TOKEN:ATTRIBUTE_3}' == 'Vicenza' && $(thisSelect).val() == '') {
			$(thisSelect).val('109').trigger('change');
		}
		if('{TOKEN:ATTRIBUTE_3}' == 'Viterbo' && $(thisSelect).val() == '') {
			$(thisSelect).val('110').trigger('change');
		}
	});
</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
2 weeks 6 days ago #222194 by achecchini
Hi tpartner!
thanks very much for the support! I try your suggestion as soon as possible.

Currently the problem is that the survey is running on 2.05 version (I know that is iper-obsolete!!) and for the moment there are no chance to change and upgrade!

Have other suggestions for my issue with a so old version?

Have a nice day.

AgoZ

Please Log in to join the conversation.

More
2 weeks 6 days ago #222220 by tpartner
I cannot test on such an old version but, from memory, try this:

<script type="text/javascript" data-author="Tony Partner">	
	$(document).on('ready' ,function(){
 
		var thisQuestion = $('#question{QID}');
		var thisSelect = $('.answer-item select:eq(0)', thisQuestion);
		var hiddenInput = $(thisSelect).nextAll('input:hidden:eq(0)');
 
		if('{TOKEN:ATTRIBUTE_3}' == 'Agrigento' && $(thisSelect).val() == '') {
			$(thisSelect).val('1');
			$(hiddenInput).val('1');
			checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
		}
		if('{TOKEN:ATTRIBUTE_3}' == 'Alessandria' && $(thisSelect).val() == '') {
			$(thisSelect).val('2');
			$(hiddenInput).val('2');
			checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
		}
 
...
 
		if('{TOKEN:ATTRIBUTE_3}' == 'Vicenza' && $(thisSelect).val() == '') {
			$(thisSelect).val('109');
			$(hiddenInput).val('109');
			checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
		}
		if('{TOKEN:ATTRIBUTE_3}' == 'Viterbo' && $(thisSelect).val() == '') {
			$(thisSelect).val('110');
			$(hiddenInput).val('110');
			checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
		}
	});
</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
1 week 6 days ago #222450 by achecchini
Hi Tony!

I try the code as suggested:

<script type="text/javascript" data-author="Tony Partner">
$(document).on('ready' ,function(){

var thisQuestion = $('#question{QID}');
var thisSelect = $('.answer-item select:eq(0)', thisQuestion);
var hiddenInput = $(thisSelect).nextAll('input:hidden:eq(0)');


if('{TOKEN:ATTRIBUTE_3}' == 'Agrigento' && $(thisSelect).val() == '') {
$(thisSelect).val('1');
$(hiddenInput).val('1');
checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
}
if('{TOKEN:ATTRIBUTE_3}' == 'Alessandria' && $(thisSelect).val() == '') {
$(thisSelect).val('2');
$(hiddenInput).val('2');
checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
}
if('{TOKEN:ATTRIBUTE_3}' == 'Ancona' && $(thisSelect).val() == '') {
$(thisSelect).val('3');
$(hiddenInput).val('3');
checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
}
if('{TOKEN:ATTRIBUTE_3}' == 'Aosta' && $(thisSelect).val() == '') {
$(thisSelect).val('4');
$(hiddenInput).val('4');
checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
}
if('{TOKEN:ATTRIBUTE_3}' == 'Arezzo' && $(thisSelect).val() == '') {
$(thisSelect).val('5');
$(hiddenInput).val('5');
checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
}

etc.

But it seem not work as aspected ...

Any other suggestion\way toresolve the issue??

Thanks in advance

Ago

Please Log in to join the conversation.

More
1 week 5 days ago #222491 by tpartner
Sorry, as I said, I cannot test on that old version.

Check for JavaScript errors in the console (F12).

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
1 week 5 days ago #222495 by Joffm
Hi,
I tested in 2.06 160129 ( I do not have an older version), used your script and I do not see an issue.
<script type="text/javascript" data-author="Tony Partner">
    $(document).on('ready', function() {
 
        var thisQuestion = $('#question{QID}');
        var thisSelect = $('.answer-item select:eq(0)', thisQuestion);
        var hiddenInput = $(thisSelect).nextAll('input:hidden:eq(0)');
 
 
        if ('{TOKEN:ATTRIBUTE_1}' == 'Agrigento' && $(thisSelect).val() == '') {
            $(thisSelect).val('1');
            $(hiddenInput).val('1');
            checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
        }
        if ('{TOKEN:ATTRIBUTE_1}' == 'Alessandria' && $(thisSelect).val() == '') {
            $(thisSelect).val('2');
            $(hiddenInput).val('2');
            checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
        }
        if ('{TOKEN:ATTRIBUTE_1}' == 'Ancona' && $(thisSelect).val() == '') {
            $(thisSelect).val('3');
            $(hiddenInput).val('3');
            checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
        }
        if ('{TOKEN:ATTRIBUTE_1}' == 'Aosta' && $(thisSelect).val() == '') {
            $(thisSelect).val('4');
            $(hiddenInput).val('4');
            checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
        }
        if ('{TOKEN:ATTRIBUTE_1}' == 'Arezzo' && $(thisSelect).val() == '') {
            $(thisSelect).val('5');
            $(hiddenInput).val('5');
            checkconditions($(thisSelect).attr('value'), $(thisSelect).attr('name'), 'select');
        }
    });
</script>

With this token list
 

and entering 444 as token I get
 

So, you'd better send your try with this question as lss export.

Joffm


 


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:

Please Log in to join the conversation.

More
1 week 5 days ago #222497 by achecchini
Hi Joffm! Thanks for your reply and test!

Unfortunately I'm still grappling with the problem and I can't get out of it.
Here -as suggested- is an extract of my application and the related csv with a test token. My trys are on the 2.05 rel.
Attachments:

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now