Welcome to the LimeSurvey Community Forum

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

javascript - get array (numeric) value from previous question

  • TonisOrmisson
  • TonisOrmisson's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago - 10 years 5 months ago #100688 by TonisOrmisson
Hi

I have a page with multiple questions and a technical hidden question grouping values based on other questions in the bottom of that page. I already regroup based on radio buttons to check which answers are checked like that:
Code:
 var q2 = 'sidXgidXqid';
 if ( $('#answer'+q2+'1').attr('checked') == true ){
   // my code here if is checked
 }

now I also need to check answers from a numerical array - I would need to check the values from the first column of each row - whether its checked (checkbox layout) or not. Can anybody help me with the array type?
Last edit: 10 years 5 months ago by TonisOrmisson.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100704 by tpartner
Assuming your X-scale sub-question codes are x1, x2, x3...

And the Y-scale codes are y1, y2, y3...

If you want to act on the state of the check-box in row-1, column-1:

Code:
var q2 = 'sidXgidXqid';
if ( $('#cbox_'+q2+'y1_x1').is(':checked')){
  // Do something
}


Note, you may want to use .is(:checked) for all of your tests as .attr('checked') has been changed to .prop('checked') in jQuery 1.6 so may not work in new versions of LimeSurvey.

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: TonisOrmisson
The topic has been locked.
  • TonisOrmisson
  • TonisOrmisson's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100730 by TonisOrmisson
Excellent, thanks!

but how would you check a radio array in a similar case?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100737 by tpartner
Code:
var q2 = 'sidXgidXqid';
if ( $('#answer'+q2+'y1-x1').is(':checked')){
  // Do something
}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • TonisOrmisson
  • TonisOrmisson's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago #100757 by TonisOrmisson
somehow i can not get the radio array to work.

in testing i tried to do also:
Code:
$('#answer'+q2+'y1-x1').change(function() {
 
            alert("test");
        });  

this interestingly worked but irregularly. Mostly did not output a message.

i attached the simple example based on the previous post - tried this both with 1.92 and 2.0 and does not seem to work.

Am I doing something wrong or ... ?
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago - 10 years 5 months ago #100758 by tpartner

somehow i can not get the radio array to work

As far as I can tell, this is working but you are calling it on page load so, as the radio is never checked, nothing happens.

Try this. If using the default template, it will give you an indication of the radio state when you click on the question text of Q1:
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function() {
      var q1 = 'sidXgidXqid';
      var q1ID = q1.split('X')[2];
    $('#question'+q1ID+' .questiontext').click(function(event) {
      if ( $('#answer'+q1+'y1-x1').is(':checked')) {
        alert('checked');
      }
      else {
        alert('un-checked');
      }
    });
  });
</script>

this interestingly worked but irregularly

The .change() function is only called when you check a radio button, not when you uncheck it by clicking another radio. You will need to put listeners on all of the radios or give them a common class.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 5 months ago by tpartner.
The following user(s) said Thank You: TonisOrmisson
The topic has been locked.
  • TonisOrmisson
  • TonisOrmisson's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 5 months ago - 10 years 5 months ago #100780 by TonisOrmisson
ok, i understand what I got wrong.
Seems to be working now.
Thanks again!
Last edit: 10 years 5 months ago by TonisOrmisson.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose