Javascript code stops working after a new test is started

More
2 years 3 weeks ago #126146 by rholguinc
rholguinc created the topic: Javascript code stops working after a new test is started
Hi there,

I really hope you can help me with this, I followed the instructions in the workarounds to use autocomplete to populate fields in another question, its a multiple short texts question type that populates another short texts question according to columns in a csv file.

The code goes as follows:
<span style="color:#328639;">5. Registrar organizaciones (OMS)</span>
<script type="text/javascript" charset="utf-8">$(document).ready(function() {
                var qID = {QID};
		var qID2 = '1714';
		var qID3 = '1716';
		var qID4 = '1717';
		var qID5 = '1718';
		var qID6 = '1719';
		var qID7 = '1720';
                var url = "http://hostname/list.csv";
                var namesArr = new Array();
                $.get(url,function(data){
                        fullArray = $.csv.toArrays(data);
                        $(fullArray).each(function(i, item){
                                namesArr.push(item[0]);
                        });
                        $('#question'+qID+' li:eq(0) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
								select: function(event, ui) {
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        $('#question'+qID2+' li:eq(0) input.text').val(item[1]);
                                                        $('#question'+qID2+' li:eq(1) input.text').val(item[2]);
                                                        $('#question'+qID2+' li:eq(2) input.text').val(item[3]);
                                                        $('#question'+qID2+' li:eq(3) input.text').val(item[4]);
                                                        $('#question'+qID2+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });
                        $('#question'+qID+' li:eq(1) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
                                select: function(event, ui) {
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        $('#question'+qID3+' li:eq(0) input.text').val(item[1]);
                                                        $('#question'+qID3+' li:eq(1) input.text').val(item[2]);
                                                        $('#question'+qID3+' li:eq(2) input.text').val(item[3]);
                                                        $('#question'+qID3+' li:eq(3) input.text').val(item[4]);
                                                        $('#question'+qID3+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });
                        $('#question'+qID+' li:eq(2) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
                                select: function(event, ui) {
                                        // Find the "ID" and "Email" values associated with the selected name value and load those questions
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        // The value from column 2 of the CSV
                                                        $('#question'+qID4+' li:eq(0) input.text').val(item[1]);
                                                        // The value from column 3 of the CSV
                                                        $('#question'+qID4+' li:eq(1) input.text').val(item[2]);
														// The value from column 4 of the CSV
                                                        $('#question'+qID4+' li:eq(2) input.text').val(item[3]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID4+' li:eq(3) input.text').val(item[4]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID4+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });
						// Initialise the autocomplete plugin
                        $('#question'+qID+' li:eq(3) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
                                // Event fired when a selection is made (ui.item.value refers to the selected item)
                                select: function(event, ui) {
                                        // Find the "ID" and "Email" values associated with the selected name value and load those questions
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        // The value from column 2 of the CSV
                                                        $('#question'+qID5+' li:eq(0) input.text').val(item[1]);
                                                        // The value from column 3 of the CSV
                                                        $('#question'+qID5+' li:eq(1) input.text').val(item[2]);
														// The value from column 4 of the CSV
                                                        $('#question'+qID5+' li:eq(2) input.text').val(item[3]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID5+' li:eq(3) input.text').val(item[4]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID5+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });	
						// Initialise the autocomplete plugin
                        $('#question'+qID+' li:eq(4) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
                                // Event fired when a selection is made (ui.item.value refers to the selected item)
                                select: function(event, ui) {
                                        // Find the "ID" and "Email" values associated with the selected name value and load those questions
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        // The value from column 2 of the CSV
                                                        $('#question'+qID6+' li:eq(0) input.text').val(item[1]);
                                                        // The value from column 3 of the CSV
                                                        $('#question'+qID6+' li:eq(1) input.text').val(item[2]);
														// The value from column 4 of the CSV
                                                        $('#question'+qID6+' li:eq(2) input.text').val(item[3]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID6+' li:eq(3) input.text').val(item[4]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID6+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });
						// Initialise the autocomplete plugin
                        $('#question'+qID+' li:eq(5) input.text').autocomplete({
                                source: namesArr,
								scroll: true,
                                // Event fired when a selection is made (ui.item.value refers to the selected item)
                                select: function(event, ui) {
                                        // Find the "ID" and "Email" values associated with the selected name value and load those questions
                                        $(fullArray).each(function(i, item){
                                                if(item[0] == ui.item.value) {
                                                        // The value from column 2 of the CSV
                                                        $('#question'+qID7+' li:eq(0) input.text').val(item[1]);
                                                        // The value from column 3 of the CSV
                                                        $('#question'+qID7+' li:eq(1) input.text').val(item[2]);
														// The value from column 4 of the CSV
                                                        $('#question'+qID7+' li:eq(2) input.text').val(item[3]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID7+' li:eq(3) input.text').val(item[4]);
														// The value from column 5 of the CSV
                                                        $('#question'+qID7+' li:eq(4) input.text').val(item[5]);
                                                }
                                        });
                                }
                        });
                });
        });
</script>
 

The script actually works, but after I start a new test it stops working, not sure why, I have checked the console and haven't found any errors. What should should I do to fix it? Thanks in advance

Please Log in to join the conversation.

More
2 years 3 weeks ago #126147 by Mazi
Mazi replied the topic: Javascript code stops working after a new test is started
What do you mean by "after I start a new test it stops working"? Please explain the work-flow on more detail.

Will this cause a problem at all for them common user who fills out the survey just once?


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
2 years 3 weeks ago #126148 by rholguinc
rholguinc replied the topic: Javascript code stops working after a new test is started
Thanks for the reply. I'm basically creating a survey that works to gather data from events referred in newspapers, so the user has to start a new test whenever he wants to register a different event. In this case the user has to fill out the same survey several times, so the second time the user attempts to fill out the same survey the code stops working.

Please Log in to join the conversation.

More
2 years 3 weeks ago #126149 by Mazi
Mazi replied the topic: Javascript code stops working after a new test is started
Weird, do you have a test link for us?


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
2 years 3 weeks ago #126181 by rholguinc
rholguinc replied the topic: Javascript code stops working after a new test is started
I found the problem, I was repeating the same variable names across subsequent code in the survey, my mistake. Sorry to bother you with this, I really appreciate your kind interest.

Please Log in to join the conversation.

More
2 years 3 weeks ago #126183 by Mazi
Mazi replied the topic: Javascript code stops working after a new test is started
You're welcome.

Have a look at donate.limesurvey.org :-)


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now