Executing multiple XMLHTTP requests within a Bootstrap Modal

My website features a Bootstrap5 Modal that loads data once the animation is complete. The issue arises when I close the modal by clicking the X in the top-right corner, the close button, or outside the modal. When I reopen the modal for another dataset (player), sometimes I receive the correct data and sometimes incorrect data.

Here is the structure of the modal:

<!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
              <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="modalTitle">Player Profile</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                  </div>
                  <div class="modal-body" id="modal-body">
                    <form>
                        <div class="row">
                            <div class="col-6 gx=2">
                                <div class="form-group">
                                    <label for="summonerName">Summoner Name</label>
                                    <input type="text" class="form-control" id="summonerName" aria-describedby="summonerName">
                                </div>
                            </div>
                            <div class="col-6 gx=2">
                                <div class="form-group">
                                    <label for="lineID">LineID</label>
                                    <input type="text" class="form-control" id="lineID" aria-describedby="lineID">
                                </div>
                            </div>
                        </div>
                        <!-- Truncated for brevity -->
                    </form>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary" onclick="savePlayerProfile()">Save</button>
                  </div>
                </div>
              </div>
            </div>

The JavaScript function responsible for refreshing the data:

function loadPlayerProfile(ID){
                var container = document.getElementById("myModal");
                var myModal = new bootstrap.Modal(container);
                console.log(myModal);
                container.addEventListener("shown.bs.modal", function() {
                    var summonerName, arenaRank, rtaRank, gkRank, bkRank, lineID, active;
                    // Truncated for brevity
                });
                myModal.show();
            }

Answer №1

After some troubleshooting, I was able to identify the issue. It turns out that every time I clicked the button, an EventHandler was added. I mistakenly assumed that the EventHandler would be replaced each time, but it was actually just being added on top of the existing one. To resolve this, I made sure to remove the previous EventHandler from the object before adding a new one.

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

Creating an HTML table that adjusts to the screen height with square-shaped cells

var grid = document.getElementById("grid"); var size = 50; for (var y = 0; y < size; y++) { var currentRow = grid.insertRow(0); for (var x = 0; x < size; x++) { var currentCell = currentRow.insertCell(-1); currentCell.style.height = currentCell.styl ...

Having trouble resolving errors in Visual Studio Code after failing to properly close a parent function? Learn how to fix this issue

Here we have the code starting with the construct function followed by the parents function public construct() //child { super.construct; } protected construct() //parent { } The issue arises where I am not receiving an er ...

Encountering difficulties when attempting to store files using mongoose in a node express.js program

I encountered an error while attempting to save a document to the MongoDB using Mongoose in my Node Express.js project. Below is the code snippet: exports.storeJob = async (req, res, next) => { const { name, email, password, title, location, descri ...

What is the best way to transfer data from a database query to Vue?

I am currently working on an API request that involves calling a SQL query within the function. I want to pass the results to a .vue page utilizing express-vue. Below is the code snippet for the API request: router.get('/search', (req, res, next ...

Bootstrap 5's Enhanced Selection Feature

Previously in Bootstrap 3, users had the capability to include the attribute data-live-search="true" in order to activate the live search feature (learn more here). Is there a way to integrate this functionality in Bootstrap 5? ...

Unable to automatically populate the final two digits of the card expiration year in MUI React

I've been working on fixing the auto-fill feature in my expiration input field. It's supposed to automatically fill in the last 2 digits of the year, just YY. Even though I think I'm doing everything correctly, the auto-fill feature keeps u ...

Assistance needed with sending JSON data to a PHP server using the POST method

I am attempting to transfer JSON data from an HTML form to a PHP server using the POST method. The issue I am encountering is that my code always ends up in the fail block within the callback function. Despite this, the Firebug console (ctrl+shift+J) does ...

Validating form field values in JavaScript prior to submission

My website features a search box that allows users to search through a database of books. When utilizing the search feature, users are presented with the following options: Search Query (a text input field) Where to search (a select field with the option ...

I need to change a website into a string so that I can analyze it with javascript. How can I do this?

Currently, I am in the process of creating a website for my video game servers. The admin tool we use outputs the current server status in a .json format as a large text string to this specific URL: My goal is to retrieve the entire text string from the p ...

One issue encountered in the AngularJS library is that the default value does not function properly in the select element when the value and

In this AngularJS example, I have created a sample for the select functionality. It is working fine when I set the default value of the select to "$scope.selectedValue = "SureshRaina";". However, when I set it to "$scope.selectedValue = "Arun";", it does n ...

A mistake occured: Unable to modify the stack property of [object Object], as it only has a getter method

Encountering an error when attempting to use a service in my component and declaring it in the constructor - TypeError: Cannot set property stack of [object Object] which has only a getter This is the code snippet: import { Component } from '@angula ...

Developing a fresh feature in Angular.js for transmitting my localstorage model information to a bus?

As a beginner in Angular Js, I have mastered the basics and am now working on storing user input values to localstorage using a form. Although this part is working fine, I need assistance in creating a new service to communicate with my colleague's . ...

Changed the form to become scrollable once it reaches a designated section during scrolling

Seeking assistance! I am currently working on a website using Bootstrap 5, and I have a registration form within the banner section, specifically within a row > col-md-5. The position of the form is fixed, but I want to make it scrollable once it reache ...

Difficulty with linking a CSS property to an HTML element using JavaScript

I'm currently working on building a custom carousel from scratch. Instead of using pre-made code snippets, I decided to challenge myself by creating one using setTimeout in JavaScript. However, I seem to be encountering an issue that I can't quit ...

Encountered a Dojo error of "TypeError {stack: (...), message: "undefined is not a function"}" when attempting to display a gif during an ajax load

I've been attempting to display a loading gif while an ajax call is in progress. However, I encountered an error at the show statement and the console displayed: TypeError {stack: (...), message: "undefined is not a function"} Here's my code sn ...

The sequence of event handler executions in JavaScript

When multiple event handlers are attached to the same event on the same elements, how does the system determine the order in which they are executed? Although I came across this thread that focuses on click events, another discussion at this page points o ...

VueJS: components unable to access global variables

I am currently in the process of transitioning my front-end to a gulp-based application. I seem to be encountering some issues with Vue.js as I am facing two errors: [Vue warn]: Property or method "params" is not defined on the instance but referenced dur ...

An issue arose in ReactJS when trying to utilize the GraphQL API

Struggling to incorporate the graphql API into a react js application. Uncertain if this approach is correct. import React, { Component } from 'react' import "./styles.css"; import axios from 'axios'; const appBaseUrl = axios.create({ ...

Angular2 - receiving an error message stating that this.router.navigate does not exist as a

Currently, I am delving into the world of Angular2 with Ionic and working on crafting a login page. However, upon loading the page, an error surfaces: 'router.initialNavigation is not a function' To address this issue, I inserted '{initialN ...

Pushing items to an array is causing the loss of previously added data

Currently, I am working on developing a prototype of a Restaurants application using Angular 8. In this project, I have implemented @Input() to transfer individual restaurant data as an object from the parent component to the RestaurantCardComponent for pr ...