Why not use getElementById? It's what it's for :)
const payBtn = document.getElementById('pay-btn')
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Morning All
I'm trying to integrate a payment gateway into my Latavel Livewire app and i'm coming up against the error Uncaught TypeError: Cannot read properties of null (reading 'addEventListener').
My JS isnt that strong so was hoping one of you kind folks might be able to assist.
For reference my input fields are set as wire:ignore.
Firstly, the proceed method is called in the component which generates a new token from the payment provider and passes this to the view via a dispatchBrowserEvent which works fine.
<div class="flex flex-wrap justify-between items-center">
@if($proceed == false)
<button wire:click="proceed" class="button button-brand text-sm w-full">Add Payment Method</button>
@elseif($proceed == true)
<div class="card-title-accent uppercase pb-1">Card Details</div>
<input wire:ignore type="text" id="hf-name" class="custom-input my-1" placeholder="Joe Smith">
<input wire:ignore type="number" id="hf-number" class="custom-input my-1" placeholder="0000 0000 0000 0000">
<div class="w-1/2">
<input wire:ignore type="number" id="hf-date" class="custom-input my-1" placeholder="00/00">
</div>
<div class="w-1/2">
<input wire:ignore type="number" id="hf-cvv" class="custom-input my-1" placeholder="000">
</div>
<button wire:ignore id="pay-btn" class="button button-brand text-sm w-full my-1">Pay now</button>
@endif
</div>
However as it goes to parse the JS i get the above typeError for this section
let hf;
const cardholderName = document.querySelector('#hf-name')
const cardNumber = document.querySelector('#hf-number')
const cardDate = document.querySelector('#hf-date')
const cardCvv = document.querySelector('#hf-cvv')
const payBtn = document.querySelector('#pay-btn')
window.addEventListener('preparePayment', function() {
.......
// Payment button submit
// Payment button submit
payBtn.addEventListener('click', () => {
startLoading()
// submits card data to pay
hf.submit({ paymentData: PAYMENT_DATA })
.then((res) => {
stopLoading()
hf.clear() // Clears payment fields (Cardholder name, Credit card number, expiration date, cvv)
showResult(true)
console.log('res', res)
})
.catch((err) => {
stopLoading()
if (err.code === 'NOT_VALID_CARD_DATA') {
showResult(false, err.message)
} else {
showResult(false, err.message)
hf.clear()
}
console.log('err', err)
})
})
.....
Please can someone advise where i might be going wrong.
Thank you in advance.
Please or to participate in this conversation.