Welcome to the LimeSurvey Community Forum

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

Change slider numeric value with javascript

  • rouuuge
  • rouuuge's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #198018 by rouuuge
hi,

does anyone has a working version of this script:
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var thisQuestion = $('#question{QID}');
 
    $('body').on('slide slideStop', '#question{QID} input.answer-item', function(event) {
      var thisName = $(this).attr('name').replace('slid', '');
 
      var otherSlider = $('input.answer-item', thisQuestion).filter(function () { 
                  return $(this).attr('name').replace('slid', '') != thisName; 
                }).first();
 
      var psSeparator = $(this).attr('data-ps-separator');
      var psStep =  $(this).attr('data-ps-step');
      var sliderVal = Number($(this).val().replace(psSeparator, '.'));
 
      // Move the slider
      var otherName = $(otherSlider).attr('name').replace('slid', '');      
      window.activeSliders['s'+otherName].getSlider().setValue(100-sliderVal);
 
      // Handle the inputs
      var displayValue = $(otherSlider).val().toString().replace('.', psSeparator); 
      $(otherSlider).val(displayValue);
      $(otherSlider).closest('li.answer-item').find('input.em_sq_validation').val(displayValue);
 
      // Fire Expression Manager
      ExprMgr_process_relevance_and_tailoring('keyup', otherName, 'change');
    });
    });
</script>

its from that old Thread but its not working with LS3.

thanks
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago - 3 years 11 months ago #198089 by tpartner
Replied by tpartner on topic Change slider numeric value with javascript
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var qID = '{QID}';
 
    // Add a custom event handler to the slider inputs
    $('body').on('slide.custom slideStop.custom', '#question'+qID+' input.answer-item', function(event) {
      handleSliders($(this));
    });
 
    function handleSliders(el) {
 
      // Remove the custom event handlers to prevent recursion (looping)
      $('body').off('slide.custom slideStop.custom', '#question'+qID+' input.answer-item');
 
      var thisName = $(el).attr('name').replace('slid', '');
 
      var otherSlider = $('#question'+qID+' input.answer-item').filter(function () { 
                  return $(this).attr('name').replace('slid', '') != thisName; 
                }).first();
 
      var psSeparator = $(el).attr('data-ps-separator');
      var psStep =  $(el).attr('data-ps-step');
      var sliderVal = Number($(el).val().replace(psSeparator, '.'));
 
      // Move the slider
      var otherName = $(otherSlider).attr('name').replace('slid', '');      
      window.activeSliders['s'+otherName].getSlider().setValue(100-sliderVal);
 
      // Handle the inputs
      var displayValue = $(otherSlider).val().toString().replace('.', psSeparator); 
      $(otherSlider).val(displayValue);
      $(otherSlider).closest('li.answer-item').find('input.em_sq_validation').val(displayValue);
 
      $(otherSlider).closest('li.answer-item').find('.slider').removeClass('slider-untouched');
 
      // Fire Expression Manager
      ExprMgr_process_relevance_and_tailoring('keyup', otherName, 'change');
 
      // Now, re-add the custom event handler to the slider inputs
      $('body').on('slide.custom slideStop.custom', '#question'+qID+' input.answer-item', function(event) {
        handleSliders($(this));
      });
    }
    });
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...4-27.lss
File Size:20 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 11 months ago by tpartner.
The following user(s) said Thank You: rouuuge
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose