views:

92

answers:

3

This is a related question to one I posted earlier... I'm trying to sum all the input elements that begin with 'pull' and place that total in the 'totalpull' input field. I have that part working. Now, I'm trying to calculate the average if a user enters something manually in the 'totalpull' input field and set each 'pull' input to that value. Trying below, but it doesn't work...

//This is the sum formula, which works
        $('input[name^=pull]').bind('keyup', function() {
                $('#totalpull').val( $('input[name^=pull]').sumValues() );
        });

        $.fn.sumValues = function() {
                var sum = 0; 
                $("input[name^='pull']").each(function() {
                        if ( $(this).is(':input') ) {
                                var val = $(this).val();
                        } else {
                                var val = $(this).text();
                        }
                        sum += parseFloat( ('0' + val).replace(/[^0-9-\.]/g, ''), 10 );
                });
                return sum;
        };


//This is the avg formula, which does not work
//Keep getting v / rowCount.replace is not a function
        $('input[name=totalpull]').bind('keyup', function() {
                $('input[name^=pull]').each(function() {
                        $(this).val( $('input[name=totalpull').avgValues() );
                });
        });
        $.fn.avgValues = function() {
                var avg = 0;
                var v = $("input[name=totalpull").val();
                var rowCount = $("#convertRow tr").length; 
                avg += parseFloat( (v / rowCount).replace(/[^0-9-\.]/g, ''), 10);
                return avg;
        }

<table id="convert">
<tbody>
<tr><td><input type="text" value="" name="pull0" /></td></tr>
<tr><td><input type="text" value="" name="pull1" /></td></tr>
<tr><td><input type="text" value="" name="pull2" /></td></tr>
<tr><td><input type="text" value="" name="pull3" /></td></tr>
</tbody>

<tfoot>
<input type="text" id="totalpull" name="totalpull" value="" />
</tfoot>
</table>
A: 

In your sumValues function, you're repeating the 'input[name^=pull]' selector. You should either pass the selector into your function, or act on the values you've already got.

In sumValues, 'this' should be a collection of inputs. Therefore, you should be able to do something like:

$.fn.sumValues = function() {
  var sum = 0;
  this.each(function() {
    sum += $(this).val();
  });
  return sum;
}

You've also got 'input' and ':input' confused. 'input' is an input tag. :input is any input or select tag.

CWF
I don't have a problem with the sum function. It's the avgValues function that I posted about.
whitman6732
Sorry, I wasn't paying as much attention as I should have.Why are you removing characters from (v/ rowCount)? It seems like that's exactly the value you want. Your regex looks funky to me because of the second '-'; I'm not sure what that's going to match. At least take that out.That said, I think what you want to do is something like:var inputs = $('input[name^=pull]');var avg = inputs.sumValues() / inputs.length;You're suffering from over-complication. Simplify the code and the solution should appear pretty quickly.
CWF
A: 

When total sum is changed manually, the value is distributed to "pull*" field:

$('input[name=totalpull]').bind('keyup', function() {
    var inp = $('input[name^=pull]');
    inp.val(parseFloat($(this).val()) / inp.length);
});​
Marko Dumic
Put that in there, and the errors went away, but still not calculating each input field...
whitman6732
Please, check your markup as I commented... I made a test case here: http://jsfiddle.net/V54gC/ can you check and see what's different from your case?
Marko Dumic
A: 

Your immediate problem is just a syntax error:

 avg += parseFloat( (v / rowCount).replace(/[^0-9-\.]/g, ''), 10);

There is no replace on a number. You need a toString thrown in:

 avg += parseFloat( (v / rowCount).toString().replace(/[^0-9-\.]/g, ''), 10 );

BTW, there's also no / for a string (like v is), but JavaScript will try to convert it for you and give you NaN if it can't.

Mark Brackett