Calculating local values when running slider function












1















I'm adding some variables in three different sliders.



Two of the sliders choose amount of apples and oranges.
These sliders multiply values from another slider (a customer slider).



When I pull the oranges and apples sliders, they get a value from the customer slider to multiply with, but the customer slider wont update values in the two other sliders.



I want the values to be calculated when I pull the customer slider as well.






var customers;

$('.input-1').bind('input', mainfunction);
$('.input-2').bind('input', mainfunction);
$('.customers').bind('input', customersfunction);

function mainfunction() {
var this_value = this.value;
var price = $(this).data("price");
var this_sum = this_value * price * customers;

$(this).closest("div").find(".counter").html(this_value);
$(this).closest("div").find(".sum").html(this_sum);
}

function customersfunction() {
customers = this.value;
$(this).closest("div").find(".counter").html(customers);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Apples: 5$</p>
<div>
<input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
<span class="counter">0</span><br>
<span class="sum">0</span>
</div>

<p>Oranges: 3$</p>
<div>
<input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
<span class="counter">0</span><br>
<span class="sum">0</span>
</div>

<p>Customers:</p>
<div>
<input class="customers" type="range" value="0" min="0" max="10">
<span class="counter">0</span><br>
</div>





My fiddle: http://jsfiddle.net/5vnrd6zx/26/










share|improve this question





























    1















    I'm adding some variables in three different sliders.



    Two of the sliders choose amount of apples and oranges.
    These sliders multiply values from another slider (a customer slider).



    When I pull the oranges and apples sliders, they get a value from the customer slider to multiply with, but the customer slider wont update values in the two other sliders.



    I want the values to be calculated when I pull the customer slider as well.






    var customers;

    $('.input-1').bind('input', mainfunction);
    $('.input-2').bind('input', mainfunction);
    $('.customers').bind('input', customersfunction);

    function mainfunction() {
    var this_value = this.value;
    var price = $(this).data("price");
    var this_sum = this_value * price * customers;

    $(this).closest("div").find(".counter").html(this_value);
    $(this).closest("div").find(".sum").html(this_sum);
    }

    function customersfunction() {
    customers = this.value;
    $(this).closest("div").find(".counter").html(customers);
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <p>Apples: 5$</p>
    <div>
    <input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
    <span class="counter">0</span><br>
    <span class="sum">0</span>
    </div>

    <p>Oranges: 3$</p>
    <div>
    <input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
    <span class="counter">0</span><br>
    <span class="sum">0</span>
    </div>

    <p>Customers:</p>
    <div>
    <input class="customers" type="range" value="0" min="0" max="10">
    <span class="counter">0</span><br>
    </div>





    My fiddle: http://jsfiddle.net/5vnrd6zx/26/










    share|improve this question



























      1












      1








      1








      I'm adding some variables in three different sliders.



      Two of the sliders choose amount of apples and oranges.
      These sliders multiply values from another slider (a customer slider).



      When I pull the oranges and apples sliders, they get a value from the customer slider to multiply with, but the customer slider wont update values in the two other sliders.



      I want the values to be calculated when I pull the customer slider as well.






      var customers;

      $('.input-1').bind('input', mainfunction);
      $('.input-2').bind('input', mainfunction);
      $('.customers').bind('input', customersfunction);

      function mainfunction() {
      var this_value = this.value;
      var price = $(this).data("price");
      var this_sum = this_value * price * customers;

      $(this).closest("div").find(".counter").html(this_value);
      $(this).closest("div").find(".sum").html(this_sum);
      }

      function customersfunction() {
      customers = this.value;
      $(this).closest("div").find(".counter").html(customers);
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <p>Apples: 5$</p>
      <div>
      <input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Oranges: 3$</p>
      <div>
      <input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Customers:</p>
      <div>
      <input class="customers" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      </div>





      My fiddle: http://jsfiddle.net/5vnrd6zx/26/










      share|improve this question
















      I'm adding some variables in three different sliders.



      Two of the sliders choose amount of apples and oranges.
      These sliders multiply values from another slider (a customer slider).



      When I pull the oranges and apples sliders, they get a value from the customer slider to multiply with, but the customer slider wont update values in the two other sliders.



      I want the values to be calculated when I pull the customer slider as well.






      var customers;

      $('.input-1').bind('input', mainfunction);
      $('.input-2').bind('input', mainfunction);
      $('.customers').bind('input', customersfunction);

      function mainfunction() {
      var this_value = this.value;
      var price = $(this).data("price");
      var this_sum = this_value * price * customers;

      $(this).closest("div").find(".counter").html(this_value);
      $(this).closest("div").find(".sum").html(this_sum);
      }

      function customersfunction() {
      customers = this.value;
      $(this).closest("div").find(".counter").html(customers);
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <p>Apples: 5$</p>
      <div>
      <input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Oranges: 3$</p>
      <div>
      <input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Customers:</p>
      <div>
      <input class="customers" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      </div>





      My fiddle: http://jsfiddle.net/5vnrd6zx/26/






      var customers;

      $('.input-1').bind('input', mainfunction);
      $('.input-2').bind('input', mainfunction);
      $('.customers').bind('input', customersfunction);

      function mainfunction() {
      var this_value = this.value;
      var price = $(this).data("price");
      var this_sum = this_value * price * customers;

      $(this).closest("div").find(".counter").html(this_value);
      $(this).closest("div").find(".sum").html(this_sum);
      }

      function customersfunction() {
      customers = this.value;
      $(this).closest("div").find(".counter").html(customers);
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <p>Apples: 5$</p>
      <div>
      <input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Oranges: 3$</p>
      <div>
      <input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Customers:</p>
      <div>
      <input class="customers" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      </div>





      var customers;

      $('.input-1').bind('input', mainfunction);
      $('.input-2').bind('input', mainfunction);
      $('.customers').bind('input', customersfunction);

      function mainfunction() {
      var this_value = this.value;
      var price = $(this).data("price");
      var this_sum = this_value * price * customers;

      $(this).closest("div").find(".counter").html(this_value);
      $(this).closest("div").find(".sum").html(this_sum);
      }

      function customersfunction() {
      customers = this.value;
      $(this).closest("div").find(".counter").html(customers);
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <p>Apples: 5$</p>
      <div>
      <input class="input-1" data-price="5" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Oranges: 3$</p>
      <div>
      <input class="input-1" data-price="3" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      <span class="sum">0</span>
      </div>

      <p>Customers:</p>
      <div>
      <input class="customers" type="range" value="0" min="0" max="10">
      <span class="counter">0</span><br>
      </div>






      jquery function slider






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 16:15









      Rory McCrossan

      245k29212248




      245k29212248










      asked Nov 14 '18 at 16:01









      Jørn Thomas DufeyJørn Thomas Dufey

      22




      22
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The issue you have is because you only update the .counter and .sum when mainfunction is called by changing a fruit slider. You also need to do this when the customers value changes as well, as the number of customers used in that calculation has changed.



          As such, it makes sense to write a common function which handles the input event of all sliders, and shows the appropriate count and sum for each of them. Something like this:






          var $sliders = $('.slider').on('input', function() {
          $sliders.each(function() {
          var $slider = $(this);
          var amount = $slider.val();

          var $container = $slider.closest('div');
          $container.find('.counter').text(amount);

          if ($slider.hasClass('fruit')) {
          var price = $slider.data('price');
          $container.find('.sum').text(amount * price * $customers.val());
          }
          });
          });

          var $customers = $sliders.filter('.customers');

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          <p>Apples: 5$</p>
          <div>
          <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
          <span class="counter">0</span><br>
          <span class="sum">0</span>
          </div>

          <p>Oranges: 3$</p>
          <div>
          <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
          <span class="counter">0</span><br>
          <span class="sum">0</span>
          </div>

          <p>Customers:</p>
          <div>
          <input class="slider customers" type="range" value="0" min="0" max="10">
          <span class="counter">0</span><br>
          </div>





          Note the use of common classes on the sliders to help genericise the logic, as well as the loop which runs through and updates all of them when any of their values has been changed.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304261%2fcalculating-local-values-when-running-slider-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The issue you have is because you only update the .counter and .sum when mainfunction is called by changing a fruit slider. You also need to do this when the customers value changes as well, as the number of customers used in that calculation has changed.



            As such, it makes sense to write a common function which handles the input event of all sliders, and shows the appropriate count and sum for each of them. Something like this:






            var $sliders = $('.slider').on('input', function() {
            $sliders.each(function() {
            var $slider = $(this);
            var amount = $slider.val();

            var $container = $slider.closest('div');
            $container.find('.counter').text(amount);

            if ($slider.hasClass('fruit')) {
            var price = $slider.data('price');
            $container.find('.sum').text(amount * price * $customers.val());
            }
            });
            });

            var $customers = $sliders.filter('.customers');

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <p>Apples: 5$</p>
            <div>
            <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
            <span class="counter">0</span><br>
            <span class="sum">0</span>
            </div>

            <p>Oranges: 3$</p>
            <div>
            <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
            <span class="counter">0</span><br>
            <span class="sum">0</span>
            </div>

            <p>Customers:</p>
            <div>
            <input class="slider customers" type="range" value="0" min="0" max="10">
            <span class="counter">0</span><br>
            </div>





            Note the use of common classes on the sliders to help genericise the logic, as well as the loop which runs through and updates all of them when any of their values has been changed.






            share|improve this answer




























              0














              The issue you have is because you only update the .counter and .sum when mainfunction is called by changing a fruit slider. You also need to do this when the customers value changes as well, as the number of customers used in that calculation has changed.



              As such, it makes sense to write a common function which handles the input event of all sliders, and shows the appropriate count and sum for each of them. Something like this:






              var $sliders = $('.slider').on('input', function() {
              $sliders.each(function() {
              var $slider = $(this);
              var amount = $slider.val();

              var $container = $slider.closest('div');
              $container.find('.counter').text(amount);

              if ($slider.hasClass('fruit')) {
              var price = $slider.data('price');
              $container.find('.sum').text(amount * price * $customers.val());
              }
              });
              });

              var $customers = $sliders.filter('.customers');

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
              <p>Apples: 5$</p>
              <div>
              <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
              <span class="counter">0</span><br>
              <span class="sum">0</span>
              </div>

              <p>Oranges: 3$</p>
              <div>
              <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
              <span class="counter">0</span><br>
              <span class="sum">0</span>
              </div>

              <p>Customers:</p>
              <div>
              <input class="slider customers" type="range" value="0" min="0" max="10">
              <span class="counter">0</span><br>
              </div>





              Note the use of common classes on the sliders to help genericise the logic, as well as the loop which runs through and updates all of them when any of their values has been changed.






              share|improve this answer


























                0












                0








                0







                The issue you have is because you only update the .counter and .sum when mainfunction is called by changing a fruit slider. You also need to do this when the customers value changes as well, as the number of customers used in that calculation has changed.



                As such, it makes sense to write a common function which handles the input event of all sliders, and shows the appropriate count and sum for each of them. Something like this:






                var $sliders = $('.slider').on('input', function() {
                $sliders.each(function() {
                var $slider = $(this);
                var amount = $slider.val();

                var $container = $slider.closest('div');
                $container.find('.counter').text(amount);

                if ($slider.hasClass('fruit')) {
                var price = $slider.data('price');
                $container.find('.sum').text(amount * price * $customers.val());
                }
                });
                });

                var $customers = $sliders.filter('.customers');

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <p>Apples: 5$</p>
                <div>
                <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Oranges: 3$</p>
                <div>
                <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Customers:</p>
                <div>
                <input class="slider customers" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                </div>





                Note the use of common classes on the sliders to help genericise the logic, as well as the loop which runs through and updates all of them when any of their values has been changed.






                share|improve this answer













                The issue you have is because you only update the .counter and .sum when mainfunction is called by changing a fruit slider. You also need to do this when the customers value changes as well, as the number of customers used in that calculation has changed.



                As such, it makes sense to write a common function which handles the input event of all sliders, and shows the appropriate count and sum for each of them. Something like this:






                var $sliders = $('.slider').on('input', function() {
                $sliders.each(function() {
                var $slider = $(this);
                var amount = $slider.val();

                var $container = $slider.closest('div');
                $container.find('.counter').text(amount);

                if ($slider.hasClass('fruit')) {
                var price = $slider.data('price');
                $container.find('.sum').text(amount * price * $customers.val());
                }
                });
                });

                var $customers = $sliders.filter('.customers');

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <p>Apples: 5$</p>
                <div>
                <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Oranges: 3$</p>
                <div>
                <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Customers:</p>
                <div>
                <input class="slider customers" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                </div>





                Note the use of common classes on the sliders to help genericise the logic, as well as the loop which runs through and updates all of them when any of their values has been changed.






                var $sliders = $('.slider').on('input', function() {
                $sliders.each(function() {
                var $slider = $(this);
                var amount = $slider.val();

                var $container = $slider.closest('div');
                $container.find('.counter').text(amount);

                if ($slider.hasClass('fruit')) {
                var price = $slider.data('price');
                $container.find('.sum').text(amount * price * $customers.val());
                }
                });
                });

                var $customers = $sliders.filter('.customers');

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <p>Apples: 5$</p>
                <div>
                <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Oranges: 3$</p>
                <div>
                <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Customers:</p>
                <div>
                <input class="slider customers" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                </div>





                var $sliders = $('.slider').on('input', function() {
                $sliders.each(function() {
                var $slider = $(this);
                var amount = $slider.val();

                var $container = $slider.closest('div');
                $container.find('.counter').text(amount);

                if ($slider.hasClass('fruit')) {
                var price = $slider.data('price');
                $container.find('.sum').text(amount * price * $customers.val());
                }
                });
                });

                var $customers = $sliders.filter('.customers');

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <p>Apples: 5$</p>
                <div>
                <input class="slider fruit" data-price="5" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Oranges: 3$</p>
                <div>
                <input class="slider fruit" data-price="3" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                <span class="sum">0</span>
                </div>

                <p>Customers:</p>
                <div>
                <input class="slider customers" type="range" value="0" min="0" max="10">
                <span class="counter">0</span><br>
                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 16:12









                Rory McCrossanRory McCrossan

                245k29212248




                245k29212248
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304261%2fcalculating-local-values-when-running-slider-function%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python