0

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

2
  • 1
    Do you mean you want "$ Other" to become selected if the user types in a custom amount? Commented Mar 31, 2016 at 18:27
  • Yes, I'm trying to get the other button to be selected as well. Commented Mar 31, 2016 at 19:23

2 Answers 2

1

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

$("#donrAmountButtons").on("click", function() {
  var amt = id.substring(10);
  setDonrAmount(amt);
  $('#donrAmount' + amt).addClass('active');
}); 

$("#donrAmountInput").on("focusout", function() {
  setDonrAmount($("#donrAmountInput").val());
  $('#otherAmount button').addClass('active');
}); 

function setDonrAmount(val) {
  var amt = val || 0;

  if (amt.length < 2 && amt < 5)
    amt = 5;

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

  $('#donrAmountButtons .active').removeClass('active');
}
Sign up to request clarification or add additional context in comments.

5 Comments

That makes a lot of sense. I was racking my head on that for a couple hours but I needed to look at it form a different perspective.. coders block...lol
The odd thing is now I can't type a number that starts with less than 5. So if I try to type 20 or 15 it wont allow me to do that. I've tried a couple things but just can't get your script to work.
Added a conditional to check the length of input. If it is two digits or greater it will ignore the check for greater than 5
Hi Ryan, I tried that a few times and for some reason it still doesn't allow any other number than 5 for the initial number. I have your code implemented at the moment where you can see that. It has me baffled because logically the conditional makes sense.
Check the code changes. The issue is with the "input" event firing when the other amount buttons are being pressed. So I've separated the two events out into button "click" and "focusout" for custom inputs.
1

You'll want to check for a keyup event on the input field, and make the selection when that happens. Something like... (inside document.ready)

$('#donrAmountInput').on('keyup', function() {
    // Probably best to strip any non-numeric characters from the field here
    amt = $(this).val();
    // pseudo-code: $('#donrAmountOther').select(); as I'm not sure about jQuery's handling of input group buttons. There'll be a way to select this one somehow!
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.