A guide on iterating through carousel elements using Vanilla JavaScript

I am currently extracting data from The Movie Database and I aim to showcase the retrieved information on my webpage using a carousel card with 3 slides. Here is a summary of my progress so far, html,

 <div class="details"><!--Movie details are supposed to be presented here--></div>

Javascript,

<script type="text/javascript">
function displayData(resultFromServer) {
  let gridHtml=" ";
let poster= "https://image.tmdb.org/t/p/w185/";
let element= document.querySelector(".details");
let index= [].indexOf.call(element.parentElement.children, element);

let selected = resultFromServer.results[index];
for (let i = 0; i < resultFromServer.results.length; i++) {
const movieResults =resultFromServer.results[i];
gridHtml += `
<div id="multi-item-example" class="carousel slide carousel-multi-item" data-ride="carousel">

<!--Indicators-->
<ol class="carousel-indicators">
<li data-target="#multi-item-example" data-slide-to="0" class="active"></li>
<li data-target="#multi-item-example" data-slide-to="1"></li>
<li data-target="#multi-item-example" data-slide-to="2"></li>
</ol>
<!--/.Indicators-->

<!--Slides-->
<div class="carousel-inner" role="listbox">

<!--First slide-->
<div class="carousel-item active">

<div class="row">
  <div class="col-md-4">
    <div class="card mb-2">
      <img class="card-img-top" src="${poster}${movieResults.poster_path}"
        alt="Card image cap">
      <div class="card-body">
        <h4 class="card-title">${movieResults.title}</h4>
        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the
          card's content.</p>
        <a class="btn btn-primary">Button</a>
      </div>
    </div>
  </div>
        </div>
        </div>
        </div>`;

}
let myPage= document.querySelector(".details");
myPage.innerHTML=gridHtml;
}
</script>

I am facing an issue where the movie details are being displayed vertically. Any assistance would be greatly appreciated!

Answer №1

Utilize querySelector to identify the presence of an 'active' class.

 function checkActiveClass(e) {
  var element = document.querySelector(".active");
  if(element !==null){
   element.classList.remove("active");
  }
 e.target.className = "active";
}

The querySelector function is employed to locate an active class within the page; if found (as indicated by the if statement), it is then removed and the active class is assigned to the subsequent element, this process continues throughout the entire collection.

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

Centering an icon in tandem with Typography text using Material UI

Is there a way to align an icon with the text so that they are on the same level? Currently, it seems like the icon is slightly above the text. I've tried using padding-top: 5px and margin-top: 5px, but those solutions didn't work as expected. ...

md-input container displays content only upon user interaction

Currently, I am working with AngularJs 1 and Angular-Material Design. One issue I encountered was with a md-input container used for file uploading. The problem I faced was that even after selecting a file, it wasn't being displayed in the md-input co ...

I am looking to showcase the data retrieved from an API by arranging it into columns with automatic data filling in a project using React.js and MySQL

When using the onKeyUp event to trigger an API call without submitting the form, I successfully fetched data in response if the mobile number matched. However, I am struggling to understand how to display this data as a default value in columns. I have tri ...

Initiate the SVG animation upon entering the viewport

I'm struggling to initiate the animation when it enters the viewport. I've looked through previous questions on stack overflow, but I can't seem to customize the JS for my specific requirements. My latest attempt was to start the pink line&a ...

Conceal/Reveal sliding from the left

I'm working on a code that allows me to hide/show content. I need the div to hide as it moves left. Does anyone know how to achieve this effect? Check out my code on FIDDLE JavaScript $(document).ready(function() { $("#button").toggle(function() ...

Guide: Looping through a typed Viewbag array - best practices

I have passed a List<AdminUsers> through the Viewbag to a view. This list is then assigned to a JavaScript variable and looped through. However, when debugging on the view, I noticed that the for loop is not being executed, even though I set a break ...

Spring MVC Analytics Trial

Hello, I am currently facing some challenges in setting up my experiment for analytics. The issue lies in the fact that I do not have different URLs, but rather different JSP pages for display purposes. For example, I have the following scenario: www.mysit ...

What is the method for extracting an entire space-separated string into a PHP variable from HTML options?

When the select tag fetches options from a MySQL database and assigns a value to a PHP variable for echoing, only the first part of the text is displayed. For example : Option: Vijay Sharma Echo Output: Vijay <select name="facultyname" oncha ...

Using dynamic jquery to target specific elements. How can we apply jquery to selected elements only?

Hello everyone! I have been working on a simple hover color change effect using jQuery, but I noticed that I am repeating the code for different buttons and service icons. Is there a way to achieve the same result so that when a button is hovered, the co ...

Exploring the process of updating the background color of a specific component in Angular

I'm currently working on a website that will feature alternating colors of white and black. However, I am facing an issue where I can only apply background color changes globally in the CSS file. Does anyone know how to address this problem? ...

Can node.js be used to extract slides from PowerPoint and convert them into images?

We are currently in the process of creating a new application that utilizes sailsjs (node + mongo) as its backend system. Our client has requested a specific feature where users can upload *.ppt files and potentially extract all slides to save them as ima ...

Endless loop issue in Reactjs encountered when utilizing React Hooks

Delving into React Hooks as a newcomer, I encountered an error that has me stumped. The console points to an infinite loop in the code but I can't pinpoint the exact line responsible. Too many re-renders. React limits the number of renders to prevent ...

jQuery plugin for uploading multiple files with validation for checking if they are empty or not

I recently downloaded the JQuery Multiple file uploader plugin from this website Before submitting the form, I need to verify whether the file input field is empty or not. <form onsubmit="return validate();"> <input type="file" name="File" class ...

If the integer holds a particular value, take one action; if not, take a different course of action

My goal is to categorize my users from 1 to 7 and display a specific message based on their rank. If the logged-in user has Rank 7, they should see a message saying "you are staff". However, I've encountered an issue where assigning Rank 7 to multiple ...

The remaining visible portion of a viewport is equivalent to the height of an element

Is there a way to dynamically set a div's height so that it expands from its starting position to the end of its parent div, which is 100% of the viewport minus 20 pixels? Here is an example of how you can achieve this using jQuery: $(document).read ...

What is the best way to ensure that my cards maintain consistent proportions?

My cards are not maintaining their proportions, causing buttons to pop out of the card and the card dimensions changing. I realize this issue is due to using fixed width and height in combination with flex. I'm struggling to find the best solution to ...

Dynamic data binding in AngularJS with dynamically generated views

The scenario: Currently, I am constructing a wizard in AngularJS that contains tabbed content with each tab representing a step in the wizard. These steps are fetched from the database through a Laravel controller and displayed using ng-repeat. Each tab c ...

Why does the CSHTML button containing a JavaScript onclick function only function intermittently?

I've implemented a download button on a webpage that dynamically assigns an ID based on the number of questions posted. Below is the code for the button: <input data-bind="attr: { id: $index() }" type="button" value="Downlo ...

Tips for utilizing ng class within a loop

Having some trouble with my template that loops through a JSON file using json server. The issue I'm facing is related to correctly applying ng class when clicking on icons. Currently, when I click on an icon, it adds a SCSS class but applies it to al ...

Tips for optimizing the rendering speed of Vuetify's v-for loop

Hi, I'm new to Vue.js and Vuetify. I am trying to display 400 columns and 6 rows with column IDs (cid) and row IDs (rid), but the rendering speed is slow. Can anyone help me improve the performance of this loop? Thank you in advance! https://i.sstatic ...