There seems to be an issue with the functionality of Js .classList.add()

Currently, I am in the process of creating a simple movie site and am working on the home page. On the home page, there is a carousel. In CSS, I have hidden the visibility of all slides, and my plan is to create an array for all the slides in JavaScript and switch between them when the arrow icon is clicked.

As a first step, I want to activate the 1st slide when the web page is loaded. I used the .classList.add() property to activate the 1st element of the array, but it's not working properly.

Please help me figure this out.

Answer №1

Give this a shot

const slides = [...av2012,...av2015,...av2018,...av2019];

The (...) in the code snippet above is known as the spread operator, which conveniently provides you with all the elements of an array.

Answer №2

When using JS and querySelectorAll to search for all occurrences, the method returns an array of results. To access the first result in the array, simply use index 0:

let left=document.querySelector('.left');
let right=document.querySelector('.right');

let av2012 = document.querySelectorAll('.Av2012')[0];
let av2015 = document.querySelectorAll('.Av2015')[0];
let av2018 = document.querySelectorAll('.Av2018')[0];
let av2019 = document.querySelectorAll('.Av2019')[0];

let slides=[av2012,av2015,av2018,av2019];

let index=0;

let active= slides[index].classList.add('active');
body{
    font-family: 'Roboto', sans-serif;
    margin: 20px 40px 0 40px;   
}

.head{
    display: flex;
    justify-content: space-between;
}

ul{
    list-style: none;
    display: flex;
    gap: 100px;  
}

#searchBar{
    width: 600px;
    height: 50px;
    border-radius: 17px;   
}

input{
    font-size: 1.5em;
    position: relative;
    left:20px;   
}
button{
border: none;
background-color: transparent;
position: relative;
left:-50px;
top: 5px;
z-index: 10; 
cursor: pointer;
}

.arrow{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100vh;
}

.left, .right{
    cursor: pointer;
    z-index: 10;
}

.right{
    transform: rotatey(180deg);
}
.movies{
    display: flex;
    justify-content: space-evenly;
    top:-550px;
    position: relative;
       
}

.active{
    visibility: visible;
}

.deactive{
    visibility: hidden;
}

.Av2012, .Av2015, .Av2018, .Av2019{
    width: 800px;
    display: flex;
    align-items: center;
    gap: 30px;
    position: absolute;
    visibility: hidden; 
}

h2, h3{
    margin-top: 0;
    margin-bottom: 0;
}

.about{margin-top: 20px;}
<body>
<div class="head">
    <div class="logo">
        <img src="Assets\logo.png" alt="website logo" width="60px" height="60px">
    </div>
    <div class="searchBar">
        <form action="search">
            <input type="text" placeholder="Find HD Movies and TV show" name="seach" id="searchBar">
            <button> <img src="Assets\search icon.svg" alt="searchIcon">
            </button>
        </form>
    </div>
    <div class="navigation">
        <ul>
            <li>Genre</li>
            <li>Year</li>
            <li>Rating</li>
        </ul>
    </div>
</div>
<div class="arrow">
    <div class="left">
        <img src="Assets\left arrow.svg" alt="go left side">
    </div>
    <div class="right">
        <img src="Assets\left arrow.svg" alt="go right side">
    </div>
</div>
<div class="movies">
    <div class="Av2012">
        <div class="poster"><img src="Assets\The Avengers 2012.jpg" alt="avengers 2012 movie poster" width="240px" height="400px"></div>
        <div class="discripction">
        <div class="title"><h2>The Avengers 2012 </h2></div>
        <div class="rating"><h3>8/10 IMDb</h3></div>
        <div class="about">Nick Fury is compelled to launch the Avengers Initiative when Loki poses a threat to planet Earth. His squad of superheroes put their minds together to accomplish the task.</div>
    </div>
</div>
<div class="Av2015">
    <div class="poster"><img src="Assets\The Avengers 2015.jpg" alt="avengers 2015 movie poster" width="240px" height="400px"></div>
    <div class="discripction">
    <div class="title"><h2>Avengers: Age of Ultron 2015 </h2></div>
    <div class="rating"><h3>7.3/10 IMDb</h3></div>
    <div class="about">Tony Stark builds an artificial intelligence system named Ultron with the help of Bruce Banner. When the sentient Ultron makes plans to wipe out the human race, the Avengers set out to stop him.</div>
</div>
</div>
<div class="Av2018">
    <div class="poster"><img src="Assets\The Avengers 2018.jpg" alt="avengers 2018 movie poster" width="240px" height="400px"></div>
    <div class="discripction">
    <div class="title"><h2>Avengers: Infinity War 2018 </h2></div>
    <div class="rating"><h3>8.4/10 IMDb</h3></div>
    <div class="about">The Avengers must stop Thanos, an intergalactic warlord, from getting his hands on all the infinity stones. However, Thanos is prepared to go to any lengths to carry out his insane plan.</div>
</div>
</div>
<div class="Av2019">
    <div class="poster"><img src="Assets\The Avengers 2015.jpg" alt="avengers 2019 movie poster" width="240px" height="400px"></div>
    <div class="discripction">
    <div class="title"><h2>Avengers: Endgame 2019</h2></div>
    <div class="rating"><h3>8.4/10 IMDb</h3></div>
    <div class="about">After Thanos, an intergalactic warlord, disintegrates half of the universe, the Avengers must reunite and assemble again to reinvigorate their trounced allies and restore balance.</div>
</div>
</div>
</div>   
    <script src="script.js"></script>
</body>

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

Content creator: Enhancing HTML search functionality using parameters

Hey there, I'm facing an issue. I created a template on my Blogger site and I need to search for text with a specific label using this URL: www.my_blog.blogspot.com/search?q=label:Printers+SearchText This is the code I have: <form action=&ap ...

Video background is malfunctioning on Firefox and Internet Explorer

Currently, I am facing some issues with the JQuery plugin 'videoBG'. The problem is that when I view the video offline, it works perfectly in all browsers. However, once I go online, the video stops working in FireFox and IE. It seems like the sc ...

Dealing with nested JSON objects in NodeJS may result in the return of [Object

I'm trying to extract nested JSON objects from a JSON array, but instead of getting the actual objects, I'm seeing [Object]. Here's an example of my JSON structure: var json = { "root_id": [ { "child-id1": { "name": "nam ...

Automatically collapse the Shadcn-UI Accordion when the mouse exits

For my self-education project, I am working on building a sidebar with an accordion using Shadcn-ui, NextJS (13.4), React, and Node. Being new to these technologies, I have encountered a problem that I can't seem to solve. The sidebar expands on mous ...

What is the best way to incorporate new elements into the DOM in order to allow users to share their comments

Can anyone help me with the code below? I have a text box and a comment section, along with a button to add comments. However, I need assistance with adding the posted comment below the comment section. Below is the code snippet: <div id="comments"&g ...

What is the best way to resize an image in HTML based on a percentage of its height?

Within my HTML code, I have a PNG image that has been adjusted in size using CSS: .adjust-image { border:1px solid #021a40; display: block; width: auto; max-width: 100%; max-height: 1000px; } .img-container { position: relative; } ...

modifying the state in a stateless React component

Hello everyone! I am here trying to work with hooks and I'm not sure how to achieve my goal. Here is the code I have: import React, { useState } from 'react' import { useForm } from 'react-hook-form' import {useDispatch } from &a ...

Aligning Description Item components horizontally in antdLearn how to easily horizontally align Description

Currently, I am utilizing the `antd` Description components. In this scenario, when there is no `title` for the items, the value should be aligned to the left. You can see an example of this alignment in the image below: https://i.sstatic.net/Ah70f.png I ...

Overflow scroll block containing a table within a div element

I am working on displaying a loop of records fetched from the database. I want to place these records inside a div block with overflow so that users can scroll through a long list easily. However, when I try to implement the below code, the appearance of t ...

Building a unique mobile number input box with country code in HTML

Struggling to design a unique mobile input box that separates the mobile number and country code with a divider. Need some assistance in creating this custom input field. Design https://i.sstatic.net/M0Wvs.png Current Implementation <div cla ...

What if there was a magical jQuery method that could automatically trigger a callback function? What could it possibly be named?

Is there a way to load only images and iframes, similar to the .load() function? I want to automatically add a selector element into the "this" variable for this purpose. $('document').ready(function({ $('a').<Something to trigg ...

Locate the cookie and perform a single redirect in PHP

I am facing an issue with redirecting to a specific page based on the presence of a cookie called "EmailSubscriber". The current condition is such that if the website detects this cookie upon loading, it will automatically redirect to a designated page. $ ...

Turn an item by x degrees, not towards x degrees

Is there a way to rotate an object by a specific degree, rather than to a specific degree? I am familiar with using transform/-webkit-transform to rotate an object by a set degree, but I am looking for a method that will rotate my object by the specified ...

Route creation unsuccessful while attempting to both download a file and send a response message, resulting in the error "Headers cannot be set after they have been sent to the client."

I'm currently working on setting up a route that will allow users to download a file and receive a response message in the HTML. I attempted to use the download function from express JS, but encountered some issues. Here is the code I am using: route ...

I am struggling to understand why clicking this button is not successfully submitting the form to Google Sheets

I am facing an issue with my Google Sheet setup that collects form data from my website. The problem is, I am unable to link the event to a button that already exists ('CompleteOrder'). Strangely, it works perfectly fine if I add a new button: & ...

Struggling with aligning a div in the center

I've been struggling to get my "boxes" div centered on my page so that everything inside that div is centered instead of being pushed to the left. Unfortunately, my CSS vision didn't turn out as expected. If someone could take a look at my jsbi ...

Uploading files via an HTML form

I am facing an issue while attempting to save a file uploaded from a form. Despite following the tutorial on the w3schools website, I keep encountering an error stating 'Undefined index: userpic' in C:\wamp\www\HW4\confirm.php ...

To include the express module in another JavaScript file, simply require it at the top of the file using the following code: `

Let me elaborate on my current project setup. I have integrated the express module in my app.js. Now, I need to access this module in other files such as index.js, admin.js, and gallery.js. However, I do not want to repeat the same code in each of these fi ...

Twitter posting unsuccessful: Issue encountered - Your current access is restricted to certain Twitter API v2 endpoints along with limited v1.1 endpoints like media post and oauth

My JavaScript app uses NPM Twit to post on Twitter. Suddenly, my bot stopped working with no explanation. It turns out that Twitter required me to switch to their new free tier and I had to delete all content from my Twitter Dev account and recreate my pro ...

Tips for resetting an angular smart filter when the reset button is clicked

Currently working on a simple Angular HTML form where I am utilizing Angular Smart Table. Can anyone advise on how to reset the smart table search filter upon clicking the reset button? Here is my HTML: <tr> <th></th> <th>< ...