www.webdeveloper.com
Results 1 to 3 of 3

Thread: jquery touch-punch sliders, autosum

  1. #1
    Join Date
    Aug 2012
    Posts
    10

    jquery touch-punch sliders, autosum

    My Javascript skills are not so good, and I need some help to make a autosum of two inputs
    I use jquery touch-punch sliders to change the values in the input boxes, but i canīt figure out
    how to autosum without a submitt button

    Code:
    <script>
            $(function() {
              $( "#slider1" ).slider({
                value:0,
                min: 0,
                max: 20,
                step: 0.1,
                slide: function( event, ui ) {
                  $( "#sum1" ).val( ui.value + " l");
                }
              });
              $( "#sum1" ).val( $( "#slider1" ).slider( "value" )  +" l");
            });
            </script>
            
            <script>
            $(function() {
              $( "#slider2" ).slider({
                value:0,
                min: 0,
                max: 10,
                step: 0.5,
                slide: function( event, ui ) {
                  $( "#sum2" ).val( ui.value + " %");
                }
              });
              $( "#sum2" ).val( $( "#slider2" ).slider( "value" )  +" %");
            });
            </script>
    
    
    <script type="text/javascript"><!--
    function updatesum() {
    document.form.sum.value = (parseFloat(document.form.sum1.value) * parseFloat(document.form.sum2.value) / 10).toFixed(2); }
    //--></script>
    
        
       <form name="form">
            <div>
            <input type="text" onChange="updatesum()" name="sum1" id="sum1" style="border:0; font-weight:bold;" />
            <div id="slider1"></div>
            </div>
    
           <div>
            <input type="text" onChange="updatesum()" name="sum2" id="sum2" style="border:0; font-weight:bold;" />
            <div id="slider2"></div>
            </div>
    
    <div>   
    <input name="sum" type="text" value="0" size="5" readonly>  
     </div>
        </form>

  2. #2
    Join Date
    Nov 2010
    Posts
    1,084
    you already have a listener for the slide event - why not update the value there, thusa removing te need for the inline listeners in your html?

    Code:
    <body>
    <script>
            $(function() {
              $( "#slider1" ).slider({
                value:0,
                min: 0,
                max: 20,
                step: 0.1,
                slide: function( event, ui ) {
                  $( "#sum1" ).val( ui.value + " l");
    			  document.form.sum.value = (parseFloat(document.form.sum1.value) * parseFloat(document.form.sum2.value) / 10).toFixed(2);
                }
              });
              $( "#sum1" ).val( $( "#slider1" ).slider( "value" )  +" l");
            });
    
            $(function() {
              $( "#slider2" ).slider({
                value:0,
                min: 0,
                max: 10,
                step: 0.5,
                slide: function( event, ui ) {
                  $( "#sum2" ).val( ui.value + " &#37;");
    			  document.form.sum.value = (parseFloat(document.form.sum1.value) * parseFloat(document.form.sum2.value) / 10).toFixed(2);
                }
              });
              $( "#sum2" ).val( $( "#slider2" ).slider( "value" )  +" %");
            });
            </script>
    
    
    
    
        
       <form name="form">
            <div>
            <input type="text" name="sum1" id="sum1" style="border:0; font-weight:bold;" />
            <div id="slider1"></div>
            </div>
    
           <div>
            <input type="text" name="sum2" id="sum2" style="border:0; font-weight:bold;" />
            <div id="slider2"></div>
            </div>
    
    <div>   
    <input name="sum" type="text" value="0" size="5" readonly>  
     </div>
        </form>
    </body>
    Last edited by xelawho; 09-01-2012 at 01:38 PM.

  3. #3
    Join Date
    Aug 2012
    Posts
    10
    Great, manny thanks !
    As I said, my javascrips skills are not so good

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles