Dave Bergschneider Dave Bergschneider - 6 months ago 21
AngularJS Question

Troubleshooting jQuery input value

I've been troubleshooting a form that allows for users to select an amount or select other amount. The other amount when click changes the amount to $5. I'm trying to get get it so that when the user changes the amount from 5 that it changes the amt = to user input.

The bit of script that changes it

function setDonrAmount(id){
var amt = id.substring(10);
if( amt == 'Other' ){ amt = 5}
var others = id.substring(0,10);
$("button[id*="+others+"]").removeClass('active');
$('button#'+id).addClass('active');
$('input#donrAmountInput').val(amt);
$('input#donrAmountInput').change();
$('#donrReviewAmount').html(amt);
}


For reference here's the actual form. Help would be greatly appreciated. https://secure.pva.org/site/c.ajIRK9NJLcJ2E/b.9381225/k.8668/FY2016_March_Congressional/apps/ka/sd/donorcustom.asp

Answer

I've made a few updates to setDonrAmount() to handle custom donations.

$("#donrAmountInput").on("input", function() {
  setDonrAmount("Other");
}); 

function setDonrAmount(id) {
  var amt;
  $('#donrAmountButtons .active').removeClass('active');

  if (id == "Other") {
    amt = $("#donrAmountInput").val();

    if ($("#donrAmountInput").val().length < 2)
      amt = amt > 5 ? amt : 5;

    $('#otherAmount button').addClass('active');
  }
  else {
    amt = id.substring(10);
    $('#donrAmount' + amt).addClass('active');
  }

  $('input#donrAmountInput').val(amt);
  $('input#donrAmountInput').change();
  $('#donrReviewAmount').html(amt);
}