- Posts: 18
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Referencing questions and subquestions in JavaScript
- ervit
- Topic Author
- Offline
- New Member
Less
More
8 years 2 months ago #130402
by ervit
Referencing questions and subquestions in JavaScript was created by ervit
Hi
I use a fair amount of javascript workarounds to manipulate my survey, but without a solid footing in JavaScript and jQuery, I often struggle with some things. There doesn't seem to be a general manual on how to reference LS questions and subquestions in JavaScript.
From some examples I know that {QID} in JS gives the question ID of the current question, but how can I reference subquestions without having to resort to the SGQA-like selectors?
For example, I currently do this to set an input mask for a subquestion in a text array:
However, if I copy the survey or move the question to another group, this code will become useless.
I'm sure there are some tricks to reference subquestions and questions without the SGQA structure, but how? At the very least I could use some code that generates this part dynamically (SGQ) so that I can be certain I don't have to rewrite the code each time I copy the survey or the question.
I use a fair amount of javascript workarounds to manipulate my survey, but without a solid footing in JavaScript and jQuery, I often struggle with some things. There doesn't seem to be a general manual on how to reference LS questions and subquestions in JavaScript.
From some examples I know that {QID} in JS gives the question ID of the current question, but how can I reference subquestions without having to resort to the SGQA-like selectors?
For example, I currently do this to set an input mask for a subquestion in a text array:
Code:
// Set the input masks $('tr.subquestion-list[id="javatbd376637X9X{QID}SQ002"] input[type="text"]', thisQuestion).inputmask('financial');
However, if I copy the survey or move the question to another group, this code will become useless.
I'm sure there are some tricks to reference subquestions and questions without the SGQA structure, but how? At the very least I could use some code that generates this part dynamically (SGQ) so that I can be certain I don't have to rewrite the code each time I copy the survey or the question.
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
8 years 2 months ago - 8 years 2 months ago #130403
by Ben_V
manual.limesurvey.org/Expression_Manager#Access_to_Variables
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Referencing questions and subquestions in JavaScript
Code:
// Set the input masks $('tr.subquestion-list[id="javatbd{SGQ}SQ002"] input[type="text"]', thisQuestion).inputmask('financial');
manual.limesurvey.org/Expression_Manager#Access_to_Variables
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Last edit: 8 years 2 months ago by Ben_V.
The following user(s) said Thank You: ervit
The topic has been locked.
- ervit
- Topic Author
- Offline
- New Member
Less
More
- Posts: 18
- Thank you received: 1
8 years 2 months ago #130405
by ervit
Replied by ervit on topic Referencing questions and subquestions in JavaScript
Thanks!
Is there a quick way to do the same for another question? E.g. by providing its QID?
Is there a quick way to do the same for another question? E.g. by providing its QID?
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
8 years 2 months ago #130406
by Ben_V
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Referencing questions and subquestions in JavaScript
There is nothing to adapt... those are dedicated keywords affecting only the question where they are pasted (either text or help area)...
This said, the possibility to mix EM {keywords} and javascript code is very limited and plain js is still required in a lot of cases
This said, the possibility to mix EM {keywords} and javascript code is very limited and plain js is still required in a lot of cases
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The topic has been locked.
- ervit
- Topic Author
- Offline
- New Member
Less
More
- Posts: 18
- Thank you received: 1
8 years 2 months ago #130408
by ervit
Replied by ervit on topic Referencing questions and subquestions in JavaScript
I see...
Is it possible to create some sort of JS function that takes a question ID as an argument and returns that question's SGA code? (I'm sure someone must have done it before)
Is it possible to create some sort of JS function that takes a question ID as an argument and returns that question's SGA code? (I'm sure someone must have done it before)
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
8 years 2 months ago #130418
by Ben_V
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Referencing questions and subquestions in JavaScript
I think it's not possible but you can manage question codes or answer text for this...
For example, you can use the following syntax to set some js based on a response to a previous radio button question:
q code: "Qgender"
2 answer options:
=> "code" (text)
a) "GIRL" (Marie-Sophie)
b) "BOY" (Jean-Pierre)
For example, you can use the following syntax to set some js based on a response to a previous radio button question:
q code: "Qgender"
2 answer options:
=> "code" (text)
a) "GIRL" (Marie-Sophie)
b) "BOY" (Jean-Pierre)
Code:
// using answer code var gender = '{Qgender.NAOK}'; if (gender == 'GIRL') { // do something } if (gender == 'BOY') { // do something }
Code:
// using answer option text var gender = '{Qgender.shown}'; if (gender == 'Marie-Sophie') { // do something } if (gender == 'Jean-Pierre') { // do something }
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The topic has been locked.
- DenisChenu
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 13643
- Thank you received: 2491
8 years 2 months ago #130444
by DenisChenu
But
- {QID} is replaced by question id (before put in HTML) then you can use $("#question{QID}").hide() (for example)
- {SGQ} is replaced by the SGQ of the answer part. answer{SGQ} for single question answer{SGQ}SQ001 for multiple question.
If you use it in another question :
{QCODE.qid} : replaced by the question id of the question with title QCODE
{QCODE.sgq} : same with SGQ
manual.limesurvey.org/Expression_Manager#Access_to_Variables
Denis
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Replied by DenisChenu on topic Referencing questions and subquestions in JavaScript
No,ervit wrote: I see...
Is it possible to create some sort of JS function that takes a question ID as an argument and returns that question's SGA code? (I'm sure someone must have done it before)
But
- {QID} is replaced by question id (before put in HTML) then you can use $("#question{QID}").hide() (for example)
- {SGQ} is replaced by the SGQ of the answer part. answer{SGQ} for single question answer{SGQ}SQ001 for multiple question.
If you use it in another question :
{QCODE.qid} : replaced by the question id of the question with title QCODE
{QCODE.sgq} : same with SGQ
manual.limesurvey.org/Expression_Manager#Access_to_Variables
Denis
Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: Ben_V
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
8 years 2 months ago #130448
by Ben_V
I never use/remember this possibility, but sounds usefull
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Referencing questions and subquestions in JavaScript
DenisChenu wrote: If you use it in another question :
{QCODE.qid} : replaced by the question id of the question with title QCODE
{QCODE.sgq} : same with SGQ
I never use/remember this possibility, but sounds usefull
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The topic has been locked.
- ervit
- Topic Author
- Offline
- New Member
Less
More
- Posts: 18
- Thank you received: 1
8 years 2 months ago #130449
by ervit
Replied by ervit on topic Referencing questions and subquestions in JavaScript
So, for example when I have this code for 'this' question:
can I have the following in the same code procedure for another question (that question's code is 'P1UnitSales'):
I'm getting errors at the above thatQuestion variable assignment
Code:
// Identify this question var thisQuestion = $('#question{QID}');
can I have the following in the same code procedure for another question (that question's code is 'P1UnitSales'):
Code:
// Identify that question var thatQuestion = $('#question{P1UnitSales.qid}');
I'm getting errors at the above thatQuestion variable assignment
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3593
8 years 2 months ago - 8 years 2 months ago #130468
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Referencing questions and subquestions in JavaScript
Try:
Code:
// Identify that question var thatQuestion = $('#question{that.P1UnitSales.qid}');
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 2 months ago by tpartner.
The following user(s) said Thank You: DenisChenu, Ben_V, ervit
The topic has been locked.
- ervit
- Topic Author
- Offline
- New Member
Less
More
- Posts: 18
- Thank you received: 1
8 years 2 months ago #130594
by ervit
I tried this approach on a multiple choice question, but the variable always turns out with some span code. In other words, this:
...comes back as this:
I need it to return just the "Y". What am I doing wrong here? I have tried .value, .code, .shown etc. - all come back with the <span> html tag.
I have found the
function which helps to strip the HTML tags as a workaround, but I'm not sure I have to resort to this... Any thoughts?
Replied by ervit on topic Referencing questions and subquestions in JavaScript
Ben_V wrote: I think it's not possible but you can manage question codes or answer text for this...
For example, you can use the following syntax to set some js based on a response to a previous radio button question:
q code: "Qgender"
2 answer options:
=> "code" (text)
a) "GIRL" (Marie-Sophie)
b) "BOY" (Jean-Pierre)
Code:// using answer code var gender = '{Qgender.NAOK}'; if (gender == 'GIRL') { // do something } if (gender == 'BOY') { // do something }Code:// using answer option text var gender = '{Qgender.shown}'; if (gender == 'Marie-Sophie') { // do something } if (gender == 'Jean-Pierre') { // do something }
I tried this approach on a multiple choice question, but the variable always turns out with some span code. In other words, this:
Code:
var ceoDir = "{MgmtPositions_SQ001.NAOK}";
Code:
"<span id='LEMtailor_Q_54_128'>Y</span>"
I have found the
Code:
$("<html>actual text</html>").text();
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1128
- Thank you received: 329
8 years 2 months ago #130597
by Ben_V
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic Referencing questions and subquestions in JavaScript
Ervit, sorry I'm a bit lost with this topic...
Nevertheless I attach you a working survey based on my previous example.
At least it shows that EM engine can interact with javascript (variables) even if I don't think that's the best way. There is probably an easier way to achieve why you need using plain javascript
Nevertheless I attach you a working survey based on my previous example.
At least it shows that EM engine can interact with javascript (variables) even if I don't think that's the best way. There is probably an easier way to achieve why you need using plain javascript
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Attachments:
The topic has been locked.