Welcome to the LimeSurvey Community Forum

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

what's the problem with my javascript ?

  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago #119967 by iscar
attached pls find the lss file, the javascript seems ok but not work,no display in q2.
for example, if you have 2 dogs, should display 8 in q2.

File Attachment:

File Name: limesurvey...1842.lss
File Size:17 KB
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #119998 by tpartner
Replied by tpartner on topic what's the problem with my javascript ?
I have no idea what you are trying to achieve but you have several JavaScript errors.

- You are trying to access an input[type="text"] where none exist
- As far as I can tell, the variable "PetNumber" is never defined
- Your .change() function should probably be applied to the number dropdown too
- In the calculation, "legPerPet" will be an invalid var (there are several elements)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago - 8 years 10 months ago #120002 by iscar
Replied by iscar on topic what's the problem with my javascript ?
-i am not sure whether input[type="text"] should be used in array number question.
- "PetNumber" is the number can be selected in q1, before the "pet type", from 1-20.
- change() function is actived when user make the selection, key problem here, i don't know how to involve selction element into function and calculation.
Code:
$(this).closest('td').find('input[type="text"]').val(legPerPet.val*PetNumber.val);
say you select dog, every dog has 4 legs(legPerPet), then total legs=PetNumber*legPerPet.if you have 2dogs, means 8 will be stored in mysql.then q1_SQ001_N1 will be displayed as 8 in q2.
q1_SQ001_N1 is calculated by legPerPet.val*PetNumber and store in mysql.
-when i select "legPerPet",how to involve into javascript?
Last edit: 8 years 10 months ago by iscar.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120003 by tpartner
Replied by tpartner on topic what's the problem with my javascript ?
Yes, I understand that you are trying to count the total pet feet. But where do you want to store that value? In the column labelled "your pet kennel"?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago - 8 years 10 months ago #120006 by iscar
Replied by iscar on topic what's the problem with my javascript ?
when user select legPerPet and PetNumber in dropdown.JS calculate legPerPet*PetNumber.the value should be store in q1_SQ001_N1,q1_SQ002_N1...q1_SQ00n_N1.
q1_SQN_N1 does not store the orginal PetNumber,but the calculated legPerPet*PetNumber.
Last edit: 8 years 10 months ago by iscar.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120008 by tpartner
Replied by tpartner on topic what's the problem with my javascript ?
To do that, I think you will need to insert two select elements - one for the pet count and one for pet type.

Then JS can be used to load the existing LimeSurvey dropdown with a calculation based on the inserted dropdown values.

Something like this:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
    var qID = {QID};
 
    // Insert the new dropdowns
    var numLegsSelect = '<select class="insertedSelect numLegs"> \
              <option value="">Choose pet type...</option> \
              <option value="4">Dog</option> \
              <option value="2">Parrot</option> \
            </select>';
    var numPetsSelect = '<select class="insertedSelect numPts"> \
              <option value="">Choose number of pets...</option> \
              <option value="1">1</option> \
              <option value="2">2</option> \
              <option value="3">3</option> \
              <option value="4">4</option> \
              <option value="5">5</option> \
              <option value="6">6</option> \
              <option value="7">7</option> \
              <option value="8">8</option> \
              <option value="9">9</option> \
              <option value="10">10</option> \
            </select>';                                
    $('#question'+qID+' .answer_cell_00N1').append(numLegsSelect).append(numPetsSelect);
 
    // Hide the LS selects
    $('#question'+qID+' .answer_cell_00N1 .multiflexiselect').hide();
 
    // Listener on the inserted dropdowns
    $('.insertedSelect').change(function() {
      var thisCell = $(this).closest('td');
      $('.multiflexiselect', thisCell).val($('.numLegs', thisCell).val()*$('.numPts', thisCell).val());
    });
  });
</script>

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: iscar
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago #120011 by iscar
Replied by iscar on topic what's the problem with my javascript ?
Thanks for tpartner professional reply!
my understanding is ls has predefined form variables and function for all type questions, people can design js based on these predefined variable like

closest('td')

.

multiflexiselect

i don't know what's the meaning of closest('td'), then i checked the manual, i didnot find predefined variables list in manual, then it's not easy to design various type question.
as the manual has lots of content,where can i get this knowledge?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120016 by tpartner
Replied by tpartner on topic what's the problem with my javascript ?
The closest .closest('td') isn't necessarily a LimeSurvey element, it is a jQuery selector for the next <td> element up the DOM structure from where it is called. So, in this case, the parent <td> element of the dropdowns with listeners on them.



Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago #120022 by iscar
Replied by iscar on topic what's the problem with my javascript ?
suppose the numLegsSelect could only be dog, value 4,make a simple sample. how to Initially select an option,involve q1_SQ001_N1/4 into the dropdown when the question has been answered?
it seemed something wrong with my script
Code:
$('#question'+qID+' .answer_cell_00N1 .multiflexiselect').each(function(i){
      if($(this).val()>0) {
                if(4==4){
        $(this).closest('td').find('.insertedSelect').val($('.numPts', thisCell).val()/4);
        $(this).closest('td').find('.insertedSelect').val($('.numLegs', thisCell).val('dog'));} else {
        $(this).closest('td').find('.insertedSelect').val($('.numPts', thisCell).val()/2);
        $(this).closest('td').find('.insertedSelect').val($('.numLegs', thisCell).val('parrot'));}
      }
    });
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago #120085 by iscar
Replied by iscar on topic what's the problem with my javascript ?
i think there's a variable mistake, but cannot find it.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 10 months ago #120110 by tpartner
Replied by tpartner on topic what's the problem with my javascript ?
Sorry, I am lost with what you are trying to accomplish.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • iscar
  • iscar's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
8 years 10 months ago #120116 by iscar
Replied by iscar on topic what's the problem with my javascript ?
i am trying use js to change the dropdown display value by:
Code:
$(this).closest('td').find('.insertedSelect').val($('.numPts', thisCell).val()/4);
        $(this).closest('td').find('.insertedSelect').val($('.numLegs', thisCell).val('dog'))
suppose in database q1_SQ001_N1==12,then the numPts will display 3? numLegs will display dog?
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose