Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david001's avatar

stripe Cannot read property 'addEventListener' of null

I am using stripe element with laravel. I got this error in console subscription:184 Uncaught TypeError: Cannot read property 'addEventListener' of null


<form action="{{ route('tubsription}}" method="post">
                            @csrf  
 <div class="card-body">

                        <div class="form-row">
                            <label for="card-element">
                                Credit or debit card
                            </label>
                            <div id="card-element" class="form-control">
                                <!-- a Stripe Element will be inserted here. -->
                            </div>

                            <!-- Used to display Element errors -->
                            <div id="card-errors" role="alert"></div>
                        </div>
</div>
                    <button class="btn btn-primary" type="submit">Process payment</button>

                    </form>

and js


 <script src="https://js.stripe.com/v3"></script>

    <script>
        
        // Create a Stripe client
        var stripe = Stripe('pk_test_no5YZwniXP4Qw4l5HKzJi3fS');
        // Create an instance of Elements
        var elements = stripe.elements();

        // Custom styling can be passed to options when creating an Element.
        // (Note that this demo uses a wider set of styles than the guide below.)
        var style = {
            base: {
                // Add your base input styles here. For example:
                fontSize: '16px',
                color: "#32325d",
            }
        };

        // Create an instance of the card Element
        var card = elements.create('card', {
            style: style
        });

        // Add an instance of the card Element into the `card-element` <div>
        card.mount('#card-element');

        // Handle real-time validation errors from the card Element.
        card.addEventListener('change', function(event) {
            var displayError = document.getElementById('card-errors');



            if (event.error) {
                displayError.textContent = event.error.message;
                console.log(event.error.message);
            } else {
                displayError.textContent = '';
            }
        });

        // Handle form submission
        var form = document.getElementById('payment-form');
        form.addEventListener('submit', function(event) {
            event.preventDefault();
            alert("ok")

            stripe.createToken(card).then(function(result) {
                if (result.error) {
                    // Inform the user if there was an error
                    var errorElement = document.getElementById('card-errors');
                    errorElement.textContent = result.error.message;
                } else {
                    // Send the token to your server
                    stripeTokenHandler(result.token);
                    //console.log(result)
                }
            });
        });

    </script>

I am getting an error subscription:184 Uncaught TypeError: Cannot read property 'addEventListener' of null On submit I got null public function store(Request $request,) { dd($request->token); }

0 likes
5 replies
apex1's avatar

Where is the id on your form?

apex1's avatar

?? Your form element doesn't have an id so of course it's going to return null if you use document.getElementById()...

Taiga's avatar
<form id="payment-form" action="{{route('tubsription}}" method="post">
var form = document.getElementById('payment-form');

You will need to use and associate the ID.

Please or to participate in this conversation.