Reset the Bootstrap carousel following an Ajax request

Assembling the carousel from the bootstrap carousel after the Ajax call has been completed. Elements and classes are being appended post page load as shown below.

async function ajaxCall() {
    let data = await fetch('ApiLink');
    let jsonData = await data.json();

    let carousel = document.createElement('section');
    let inner = document.createElement('div');
    carousel.id = 'slider';
    carousel.className = 'carousel carousel-dark slide';
    carousel.setAttribute('data-bs-ride', 'carousel');
    inner.className = 'carousel-inner';

    jsonData.data.forEach(data => {
        let item = document.createElement('div');
        let avatar = document.createElement('img');
        item.className = 'carousel-item';
        avatar.src = data.avatar;
        item.appendChild(avatar);
        inner.appendChild(item);
    });

    inner.querySelector('.carousel-item').classList.add('active');
    carousel.appendChild(inner);
    document.body.appendChild(carousel);
}

Despite these efforts, the carousel fails to function properly.

It appears that reinitializing the carousel after the creation and appending of HTML elements may be necessary. Hence, the question arises:

How can the bootstrap carousel be reinitialized following the Ajax call without utilizing jQuery?

Answer №1

According to the official documentation, creating a carousel instance involves using the carousel constructor after setting up your slider element like this:

let carousel = new bootstrap.Carousel(slider, {});

It's also important to familiarize yourself with the available carousel methods. For instance, the getOrCreateInstance method can either retrieve a existing carousel instance associated with a specified DOM element or create a new one if it doesn't already exist, as shown below:

bootstrap.Carousel.getOrCreateInstance(slider, {});

Remember, you have the flexibility to add any desired attributes to your slider in the second parameter.

Additionally, note that the data-bs-ride attribute is only read once during page loading, so there's no need to configure it in your Ajax requests.

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

Dropzone is encountering an issue with uploading photos: The upload path seems to be invalid

Despite thoroughly checking the path settings in my code, the error message persists when I attempt to upload photos using Dropzone. It seems to be indicating a problem with the upload path. What could be causing this issue? I am currently in the process ...

I'm new to Angular and I'm wondering how to close the panel by clicking on the 'x' button and also by clicking on the screen. Can anyone help me with this

Below is the HTML code I use for my button: <button class="btn btn-outlined " ng-click="vm.showCommentBox1()">Notify All</button> <div class="comment-box custom saveAll" ng-if=""><div class="panel panel-default"> ...

Failure to send chat form

I have been developing a PHP chat application and everything is working well. However, I am looking to implement AJAX in order to prevent the page from refreshing. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></s ...

The functionality of Javascript on a website is not functioning properly when accessed through a selenium script

Why isn't Javascript functioning when I access the site through a web driver script? Here is the link to the site: . The code snippet is as follows: WebDriver d=new FirefoxDriver(); d.get("http://www.formget.com/tutorial/register_demo/registr ...

Using AngularJS to dynamically populate a dropdown menu from a JSON object

I am a beginner to Angular and currently facing my first significant challenge. The JSON object below is the address data retrieved from a third-party API and added to $scope.AddressData in a controller: $scope.AddressData = [{ "Address1":"South Row", ...

The function and if-else statement are experiencing malfunctions

Currently in the early stages of learning coding, I've been focusing on building a solid foundation by practicing with CodeWars. Utilizing this platform for practice has been beneficial because it offers solutions for guidance. While attempting to wor ...

Converting Dynamo DB stream data into Json format

I need to convert the DDB stream message into a standard JSON format. To achieve this, I am using unmarshalleddata = aws.DynamoDB.Converter.unmarshall(result.NewImage); where result.NewImage is { carrier: { S: 'SPRING' }, partnerTransacti ...

Ways to hide the value from being shown

I have integrated jscolor (from jscolor) to allow users to pick a color with an input field. However, I am facing issues in styling it as I'm unable to remove the hex value of the selected color and couldn't find any relevant documentation on av ...

Decoding JSON in AngularJS

Looking for assistance in parsing a JSON 2D array with Angular JS and fetching images from the array. Can anyone provide some guidance on this? I have updated my Plunker at this link HTML Code <!DOCTYPE html> <html lang="en" ng-app="myAp ...

Opting to utilize a multidimensional JavaScript array or object is a more efficient method

Here is a breakdown in JSON format: jsonResponse Folder 01 Product 01 - Folder 01 Product 02 - Folder 01 Product 03 - Folder 01 Folder 02 Product 01 - Folder 02 Product 02 - Folder 02 Product 03 - Fo ...

Looking to add tiered custom attribute select boxes in SnipCart - how can I make it happen?

I am interested in implementing two custom attributes for products, where the options for the second custom attribute are determined by the selection of the first attribute. My specific situation involves selling art in various formats and sizes, with dif ...

Adjusting the date format within an AJAX success callback: alter how the date is displayed

Attempting the following code: var second_date = moment(currentdate).format('DD-MM-YYYY'); The result is: Uncaught Reference Error: moment is not defined. success: function(response){ var len = 0; if(response != n ...

What could be causing the malfunction of the v-bind attribute?

I am in the process of developing a straight-forward To-Do List application with VueJS. <template> <div> <br/> <div id="centre"> <div id="myDIV" class="header"> <h2 style="margin:5px">M ...

Guide to retrieve the Last-Modified date using Javascript

It is common knowledge that the document.lastModified function returns a string containing the date and time when the current document was last modified. Is it possible to obtain the Last-Modified for a script? Here is an example of HTML code: ... <s ...

Determining the file size of an HTML and JavaScript webpage using JavaScript

Is there a way to determine the amount of bytes downloaded by the browser on an HTML+JS+CSS page with a set size during page load? I am looking for this information in order to display a meaningful progress bar to the user, where the progress advances bas ...

Just one piece of information was successfully added to the food ordering system database

As a beginner in using Ajax with PHP, I am seeking assistance. My issue revolves around a table called "table_menu" consisting of 5 columns: table_menu: menu_id menu_foodname menu_price menu_quantity menu_image In each of these columns, there ...

What is the process for loading a font file in Vue.js and webpack?

I've done a lot of research, but I couldn't find any links that show me exactly how to add fonts in VueJS. This is the method I'm using to import the font in my LESS file: @font-face { font-family: "Questrial"; src: url("../../fonts/Que ...

I need assistance in locating an error; the error message states that $ is not defined and an object is expected

Issue with $ not being defined, object expected.. I am trying to verify if all sets of radio buttons are checked when a button is clicked! Please help. <script type="text/javascript> $(document).on('click', 'form', function () { ...

The header row in my table multiplies each time an AJAX call is made

HeaderRowMultiplesEachTimeAjax My web grid table in JavaScript consists of a complete HTML table. The top header, which acts like a colgroup caption table, is built through JavaScript insertion as shown below: var headerRow = "<tr><th colspan=&a ...

Retrieving attribute values when using the .on function in jQuery

I currently have 10 links with the following format: <a href="#" data-test="test" class="testclass"></a> as well as a function that looks like this: $(document).on("click", ".testclass", function () { alert($(this).attr('data-t ...