This is my code below. One can dynamically add fields by clicking 'Add More' button & also can remove the fields if required. I need to multiply input values and to get the Grand Total of the results of multiplication of successive fields. How can i achieve this in Jquery?
<div class="my-form">
<form name="form" >
<p class="text-box">
<select name="size[]" id="box1">
<option value="20">20</option>
<option value="35">35</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
x
<input name="qty[]" id="box2" type="text" value="" />
= <input name="multiply[]" id="answer" type="text" value="" />
<a class="add-box" href="#">Add More</a>
</p>
<input type="submit" value="Submit" />
</form>
</div>
Grand Total: <span class="GrandTotal">0.00</span><br/><br/>
<script>
jQuery(document).ready(function($){
$('.my-form .add-box').click(function(){
var n = $('.text-box').length + 1;
if( 10 < n ) {
alert('Stop it!');
return false;
}
var box_html = $('<p class="text-box"> <select name="size[]" id="box1' + n + '"> <option value="20">20</option> <option value="35">35</option> <option value="50">50</option> <option value="100">100</option> </select> x <input type="text" name="qty[]" value="" id="box2' + n + '" /> = <input name="multiply[]" id="answer' + n + '" value="" /> <a href="#" class="remove-box">Remove</a></p>');
jQuery('#my-form').append(box_html);
box_html.hide();
$('.my-form p.text-box:last').after(box_html);
box_html.fadeIn('slow');
return false;
});
$('.my-form').on('click', '.remove-box', function(){
$(this).parent().css( 'background-color', '#FF6C6C' );
$(this).parent().fadeOut("slow", function() {
$(this).remove();
$('.box-number').each(function(index){
$(this).text( index + 1 );
});
});
return false;
});
});
</script>