Tommy L
Tommy L

Reputation: 138

Django - Python: Get sum from 2 fields

In html template I have 3 fields (defined in models.py): A, B and C. It is a form. I want field C to have a sum of A + B when user types some value.
Is that something I can do with python or it is necessary JavaScript?

Upvotes: 0

Views: 907

Answers (1)

George K
George K

Reputation: 499

You will have to use JavaScript since it's in the front-end. Setup a jQuery listener so that once elements A & B are modified the value C is also modified.

$('#A, #B').on('input',function(e){
    $('#C').val(parseInt($('#A').val()) + parseInt($('#B').val()));
});

https://jsfiddle.net/4c1jd6ar/

Upvotes: 1

Related Questions