[jQuery] Re: Building a weight calculator, completely stumped

2009-02-02 Thread Zaliek

Thank you! Works perfect!


[jQuery] Re: Building a weight calculator, completely stumped

2009-02-02 Thread Eric Garside

You could significantly speed things up by throwing a class on all
your input classes.







$('#calculateweight').click(function(){
var sum = 0;
$('input.weighting').each(function(){
sum += $(this).val()*1;
});
$('#totalweight').text( Math.ceil(sum) );
return false;
});

On Feb 2, 4:26 pm, Stephan Veigl  wrote:
> Simply use an each loop:
>
>     $("#calculateweight").click(function() {
>        var sum = 0;
>        $("input[name*='weight']").each(function() {
>          sum += Number($(this).val());
>        });
>        $("#totalweight").text( Math.ceil(sum) );
>         return false;
>     });
>
> @James: looks quite similar :-)
> However you should reset the total within the click function,
> otherwise you would increase the sum with every click
>
> by(e)
> Stephan
>
> 2009/2/2 Zaliek :
>
>
>
> > I'm new to javascript and jquery.
> > I'm building a shipping calculator that pulls the weights from hidden
> > input fields and adds them together then calculates the shipping
> > price. Due to the proprietary nature of the code I cannot change the
> > code of the input fields. The input fields are named weight1, weight2,
> > etc and the total number of input fields varies depending on the
> > number of items in the shopping cart.
>
> > The below code is a mockup webpage meant for designing the script:
> > ---
> > 
> > 
> > 
> > 
> > $(document).ready(function() {
> >     $("#calculateweight").click(function() {
> >        var weightarray = $("input[name*='weight']").val();
> >     });
> > });
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > Calculate Weight
> > 
> > 
> > 
> > ---
> > When the Calculate Weight link is clicked the script should pull the
> > weights, add them together then do Math.Ceil to round up the result
> > and then pass that to the next part of the script that will compare
> > that weight to a table of weights and prices.
>
> > My problem currently is that I have no idea how to add together the
> > array of weights to pass them into a variable which I can then run
> > Math.Ceil on.
>
> > Any help is much appreciated!


[jQuery] Re: Building a weight calculator, completely stumped

2009-02-02 Thread Stephan Veigl

Simply use an each loop:

$("#calculateweight").click(function() {
   var sum = 0;
   $("input[name*='weight']").each(function() {
 sum += Number($(this).val());
   });
   $("#totalweight").text( Math.ceil(sum) );
return false;
});


@James: looks quite similar :-)
However you should reset the total within the click function,
otherwise you would increase the sum with every click

by(e)
Stephan


2009/2/2 Zaliek :
>
> I'm new to javascript and jquery.
> I'm building a shipping calculator that pulls the weights from hidden
> input fields and adds them together then calculates the shipping
> price. Due to the proprietary nature of the code I cannot change the
> code of the input fields. The input fields are named weight1, weight2,
> etc and the total number of input fields varies depending on the
> number of items in the shopping cart.
>
> The below code is a mockup webpage meant for designing the script:
> ---
> 
> 
> 
> 
> $(document).ready(function() {
> $("#calculateweight").click(function() {
>var weightarray = $("input[name*='weight']").val();
> });
> });
> 
> 
> 
> 
> 
> 
> 
> 
> Calculate Weight
> 
> 
> 
> ---
> When the Calculate Weight link is clicked the script should pull the
> weights, add them together then do Math.Ceil to round up the result
> and then pass that to the next part of the script that will compare
> that weight to a table of weights and prices.
>
> My problem currently is that I have no idea how to add together the
> array of weights to pass them into a variable which I can then run
> Math.Ceil on.
>
> Any help is much appreciated!
>


[jQuery] Re: Building a weight calculator, completely stumped

2009-02-02 Thread James

Something like (untested):

var total = 0;

$("#calculateweight").click(function() {
   $("input[name^='weight']").each(function() {
total += $(this).val();
   });
   alert('total');
   return false;  // so the  link doesn't go through
});

This won't work well if you have other input elements with name
beginning with "weight" that's not part of the calculation.
You'll also probably want to add some typecasting to change the values
to a float/int.



On Feb 2, 9:51 am, Zaliek  wrote:
> I'm new to javascript and jquery.
> I'm building a shipping calculator that pulls the weights from hidden
> input fields and adds them together then calculates the shipping
> price. Due to the proprietary nature of the code I cannot change the
> code of the input fields. The input fields are named weight1, weight2,
> etc and the total number of input fields varies depending on the
> number of items in the shopping cart.
>
> The below code is a mockup webpage meant for designing the script:
> ---
> 
> 
> 
> 
> $(document).ready(function() {
>      $("#calculateweight").click(function() {
>         var weightarray = $("input[name*='weight']").val();
>      });});
>
> 
> 
> 
> 
> 
> 
> 
> 
> Calculate Weight
> 
> 
> 
> ---
> When the Calculate Weight link is clicked the script should pull the
> weights, add them together then do Math.Ceil to round up the result
> and then pass that to the next part of the script that will compare
> that weight to a table of weights and prices.
>
> My problem currently is that I have no idea how to add together the
> array of weights to pass them into a variable which I can then run
> Math.Ceil on.
>
> Any help is much appreciated!