Overriding a variable derived from val ()

In the following code, how could I override the "donateAmount" variable whenever someone runs the click function?

In other words, whenever someone clicks, he removes everything that is inside the variable "donateAmount" and replaces the new sum val ().

thanks

UPDATE

This code below now works, thanks to fixing the problem with the variable scope.

$('label input').click(function () { if ($('label input').is(':checked')) { $(this).parent().addClass('checked'); $('.donate-now label').not($(this).parent()).removeClass('checked'); var donateAmount = $(".checked input").val(); } $('#click').click(function(){ if ($('label #other').is(':checked')) { donateAmount = $("#otherAmount").val(); } $('p').html(donateAmount); }); }); 
+4
source share
1 answer

try it

  var donateAmount = $(".checked input").val(); $('label input').click(function () { var $this = $(this); if ($this.is(':checked')) { $this.closest('label').addClass('checked'); $('.donate-now label').not($this.closest('label')).removeClass('checked'); donateAmount = $this.val(); if ($('#other').is(':checked')) { donateAmount = $("#otherAmount").val(); } } }); $('#otherAmount').change(function() { donateAmount = this.value; }); $('#click').click(function () { $('p').html('$' + donateAmount); }); 

Check script

The method for determining the event is click $('#click').click(function () will bind the event several times, once for each click.

Move the event beyond the click event.

Also try using .on to bind event handlers.

.click(function () { changes to .on('click', function () {

0
source

All Articles