Error message: Modal.dispose function not available in Bootstrap 5 Symfony 5.3 issue detected

I have been working on creating a modal using Symfony 5 and Bootstrap 5. I want the form fields to be cleared when the modal is closed. Below are the relevant files. I am encountering an error when trying to close the modal: Uncaught TypeError: myModal.dispose is not a function. Any assistance would be greatly appreciated.

app.js

import './styles/app.scss';

// start the Stimulus application
import './bootstrap';

import { Tooltip, Toast, Popover, Modal} from 'bootstrap';
import clearModalFields from './js/clearModalFields.js'

clearModalFields.js

var myModal = document.getElementById('createButtonForm');

myModal.addEventListener('hide.bs.modal', function () {
    myModal.dispose()
})

createButtonForm.html.twig

<body>
{{ form_start(formButton) }}
    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createButtonForm">Create a Button</button>

    <div class="modal fade" id="createButtonForm" tabindex="-1" aria-labelledby="createButtonFormLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="createButtonFormLabel">Create a Button</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            <form>
                <div class="mb-3">
                    <label class="col-form-label">Label :</label>
                    {{ form_widget(formButton.label, {'attr': {'class': "form-control", 'autofocus': true}}) }}
                    {{ form_errors(formButton.label)}}
                </div>

                <div class="mb-3">
                    <label class="col-form-label">Link :</label>
                    {{ form_widget(formButton.link, {'attr': {'class': "form-control"}}) }}
                </div>

                <div class="mb-3">
                    <label class="col-form-label">Button Type :</label>
                    {{ form_widget(formButton.permissionType, {'attr': {'class': "form-control"}}) }}
                </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
            {{ form_widget(formButton.submit, {'attr': {'class': "btn btn-primary"}}) }}
            {{ form_widget(formButton._token) }}
          </div>
        </div>
      </div>
    </div>
    <br><br>
  {{ form_end(formButton) }}
</body>

Answer №1

When using the getElementById method, it retrieves an Element object that does not have a dispose function. To access the Modal object and perform the necessary actions, you should utilize the getInstance() method.

import { Modal } from 'bootstrap';

var myModal = document.getElementById('createButtonForm');

myModal.addEventListener('hide.bs.modal', function () {
    Modal.getInstance(myModal).dispose()
})

Answer №2

$('#modalId').on('hidden.bs.modal', function () {
    $(this).find('form').trigger('reset');
})

Give this a shot, it worked wonders for my situation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In ES6 React, if you want to add arguments to event handlers when functions are bound in the constructor, follow these

When working with constructors in es6, it is recommended to bind functions early like so: class App extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); // binding done in the constructor ...

develop a hidden and organized drop-down menu using the <select> tag

I am currently developing a website for a soccer league. The site includes two dropdown lists with specific criteria, where the options in the second dropdown are limited based on the selection made in the first dropdown. My goal is to initially hide cer ...

I noticed that my jquery code is injecting extra white space into my HTML5 video

Ensuring my HTML5 background video stays centred, full-screen, and aligned properly has been made possible with this jQuery snippet. $(document).ready(function() { var $win = $(window), $video = $('#full-video'), $videoWrapper = $video. ...

Working with iFrames through Splinter/Selenium in Python

Just a heads up: I can work with either Selenium or the Splinter API wrapper for Selenium! I've encountered some hurdles while trying to navigate iframes on Twitter.com using the Python Splinter API. For instance, with Browser('firefox', ...

Unable to locate the variable named StyleSheet

I've been delving into React Native using this informative site https://www.tutorialspoint.com/react_native/react_native_animations.htm However, I encountered a setback when attempting to launch the app on my iPhone. An error message indicates that a ...

Issue with ajax form: success function is not functioning as intended

For my form submission, I am utilizing ajaxForm(options) to run some functions before submitting the form. Here is an example of the options that I have configured: var options = { target : '#output1', success : ...

The Window.print() function may experience compatibility issues across different browsers

When attempting to utilize the Window.print() function, I encountered an issue where it works perfectly in Google Chrome but not in Mozilla Firefox. Attached are screenshots displaying the problem at hand. What could be causing this discrepancy? Furthermor ...

Conflicting Angular controller names within different modules

I'm facing an issue where two modules (A and B) with controllers of the same name are conflicting when imported into module C. Is there a recommended solution to prevent this conflict, such as using a naming convention like "module.controller" for ea ...

Which is better for scrolling in Angular 2+: using HostListener or window.pageYOffset?

Which syntax is best for maximizing performance in Angular 2+? Is it necessary to use HostListener, or is it simpler to obtain the scroll position using the onscroll window event and pageYOffset? @HostListener('window:scroll', ['$event&ap ...

Interested in learning how to code games using JavaScript?

Hi everyone, I'm currently exploring the world of Javascript and have been tasked with completing a game for a class project. The game involves a truck that must catch falling kiwis while only being able to move left or right. A timer is set for two m ...

What is the purpose of the 'onClassExtended' function in Extjs 6 for class definition?

Ext.define('Algorithm.data.Simulated', { needs: [ //.... ], onClassExtended: function(obj, info) { // .... } }) I came across this code snippet but couldn't locate any official documentation for it on Sencha ...

What is the reason for Bower consistently choosing to download the Angular version 1.5.9-build.5086+sha...?

Struggling to manage my front end dependencies using bower.json. No matter how I specify the version of Angular in bower, a different version is downloaded every time. The issue is that many functionalities in my code rely on previous versions of Angular, ...

PHP variables that are constantly changing

I recently developed a website that showcases random entries from a database to viewers. Here is the current code snippet I am using: $records = "SELECT * FROM xxx"; $records_query = mysql_query($records); $num_records = mysql_num_rows($records_query); $i ...

Sending array data from the client-side JavaScript to the Spring MVC controller via AJAX

Is there a way to send an array from JavaScript in a web browser to a Spring MVC controller using AJAX? Here's the JavaScript code I have: var a = []; a[0] = 1; a[1] = 2; a[2] = 3; // Is it possible to send multiple arrays as well? $.ajax({ typ ...

XDomainRequest for cross-domain ajax is throwing an error that is difficult to understand - an empty error message

Here is my AJAX call to a page on a different domain: if ($.browser.msie && window.XDomainRequest) { // Use Microsoft XDR var xdr = new XDomainRequest(); xdr.open("post", "https://different-domain.aspx"); ...

Conceal a div and label after a delay of 5 seconds with a JavaScript/jQuery function in C#

Here is a sample div: <div class="alert alert-success alert-dismissable" runat="server" visible="false" id="lblmsgid"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> ...

Incorporate personalized design elements within the popup component of Material-UI's DataGrid Toolbar

I am in the process of customizing a Data Grid Toolbar component by making adjustments to the existing Grid Toolbar components sourced from Material-UI. For reference, you can view the official example of the Grid Toolbar components here. Upon clicking o ...

Using React Quill JS and looking to log to the console when a change handler is triggered?

As I dive into the world of web development, I am currently working on crafting a blog dashboard. For the text editor, I've opted to use React Quill. While following the documentation, I came across a tutorial that includes an 'on change' ha ...

Ways to customize a bot's status based on the time of day

I'm currently working on enhancing my discord bot by having its status change every hour for a more dynamic user experience. However, I'm facing challenges with JavaScript dates. Can anyone provide some guidance? Below is the snippet of code I ha ...

Adjust the CSS property of a div to have a fixed position when scrolling

I attempted to implement a fixed div on scroll in Vue.js using the following code: ... async created() { window.addEventListener('scroll', this.calendarScroll); } methods: { calendarScroll() { console.log('Scroll'); cons ...