Skip to main content

Using jquery.calculation.js with dynamically added form fields



I'm creating a form with dynamically added fields and would like to sum all added fields. So far I have the following code, but it only sums the first row:







<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Totals</title>

<script type="text/javascript" src="../js/jquery/jquery-1.6.2.js"></script>

<script type="text/javascript" src="../js/jquery/jquery.calculation.js"></script>



<script type="text/javascript">

//Sum

$(document).ready(

function (){

$('input[name^=reds]').sum("keyup", "#totalSumReds");

$("input[name^=blues]").sum("keyup", "#totalSumBlues");

}

);

</script>



<script type="text/javascript">

//Add/remove form fields

jQuery(function($){

$('#btnAdd').click( function(){

var num = $('.clonedInput').length;

var newNum = new Number(num + 1);

var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);



newElem.children(':first').attr('id', 'name' + newNum).attr('reds', 'reds' + newNum);

$('#input' + num).after(newElem);

$('#btnDel').attr('disabled', false);

if (newNum == 5)

$('#btnAdd').attr('disabled', 'disabled');

});



$('#btnDel').click( function() {

var num = $('.clonedInput').length;

$('#input' + num).remove();

$('#btnAdd').attr('disabled', false);

if (num-1 == 1 )

$('#btnDel').attr('disabled', 'disabled');

});



$('#btnDel').attr('disabled', 'disabled');

});

</script>

</head>



<body>

<form id="myForm">

<div id="input1" style="margin-bottom:4px;" class="clonedInput">

Numbers: <input type="text" name="reds"/>

<input type="text" name="blues" id="blues"/>

</div>

<div>

Totals: <input type="text" name="totalSumReds" id="totalSumReds" value="" size="2" readonly="readonly" />

<input type="text" name="totalSumBlues" id="totalSumBlues" value="" size="2" readonly="readonly" />



</div>

<div>

<input type="button" id="btnAdd" value="add line" />

<input type="button" id="btnDel" value="remove line" />

</div>

</form>

</body>

</html>







Any ideas about what should I try to fix it? Thanks!


Comments

  1. In your code you are registering the summing function only on the existing input fields, any fields added afterwards won't trigger the calculation and also won't be taken into account when summing.

    You could use event delegation instead:

    <script type="text/javascript">
    // Sum
    $(function (){
    $('#myForm').live("keyup", "input[name^=reds]", function() {
    var sum = $('input[name^=reds]').sum();
    $('#totalSumReds').val(sum);
    });
    $('#myForm').live("keyup", "input[name^=blues]", function() {
    var sum = $('input[name^=blues]').sum();
    $('#totalSumBlues').val(sum);
    });
    });
    </script>

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.