I am trying to set a value for the textbox on a PayPal Giving Fund donation page and am thoroughly stumped.
Here's what I've tried:
Setting input.value
From Chrome devtools, if I run the following JavaScript:
document.getElementById('nemo_inputAmount').value = '1'
The value '1' appears in the textbox, but when I click the "Donate Now" button, the value is cleared and the form does not submit.
Sending events
I used Chrome devtools to monitor all of the events when I tabbed into the textbox, typed the value '1', and tabbed out.
Then I recreated each of those events using the code below. The value '1' does not even appear in the textbox using this approach. I have tried every combination and style of dispatching events that I can think of.
function textEvent(text) {
let textEvent = document.createEvent('TextEvent');
textEvent.initTextEvent('textInput', true, true, null, text);
return textEvent;
}
function setDonationAmount(amount) {
let amountInput = document.querySelector('input#nemo_inputAmount');
if (amountInput) {
console.log('Setting amount to', amount)
const events = [
new FocusEvent('focus', {
composed: true
}),
new Event('select', {
bubbles: true
}),
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
composed: true,
code: 'Digit1',
key: '1',
keyCode: 49,
which: 49
}),
new KeyboardEvent('keypress', {
bubbles: true,
cancelable: true,
composed: true,
code: 'Digit1',
key: '1',
keyCode: 49,
which: 49
}),
textEvent('1'),
new Event('input', {
bubbles: true
}),
new KeyboardEvent('keyup', {
bubbles: true,
cancelable: true,
composed: true,
code: 'Digit1',
key: '1',
keyCode: 49,
which: 49
}),
new Event('change', {
bubbles: true
}),
new FocusEvent('blur', {
composed: true
})
]
events.forEach(function(event) {
amountInput.dispatchEvent(event);
})
}
}
setDonationAmount('1');
As you can see, I'm trying to do this in pure JavaScript. I'm completely baffled why neither of the solutions above work.
Does anyone have any suggestions?