Giving numerous divs identical attributes

Currently working on developing a social media platform similar to Instagram. Encountered an issue while implementing the story feature. Upon opening the story, I created lines using div elements with a common class for CSS styling purposes. However, I introduced an active id that does not apply to all of them.

The ones with the active id are intended to be displayed in white with full opacity (1), while those without the id should have an opacity of 0.5

Initially, applying the active id worked as expected for a single element. But upon assigning the second id, only the first one remained affected.

const active = document.getElementById("active");

active.style.backgroundColor = "white";
active.style.opacity = 1;
.flex-container {
  display: flex;
  flex-direction: row;
}

.flex-container-item {
  background-color: rgb(255, 255, 255);
  opacity: 0.5;
  padding: 1px;
  flex: 1pt;
  margin-right: 5px;
}
<div class="flex-container">
  <div class="flex-container-item" id="active">A</div>
  <div class="flex-container-item">B</div>
  <div class="flex-container-item">C</div>
  <div class="flex-container-item">D</div>
</div>

Answer №1

Avoid using duplicate ID values for different HTML elements. Instead, assign the "active" class to the element and access it via JavaScript as shown below:

const activeElements = document.getElementsByClassName("active");

Answer №2

Having duplicate ids is not allowed since each id should be unique. Instead, consider creating an active CSS class like this:

.active {
   background-color: white;
   opacity: 1;
 }

You can then apply this class conditionally to the divs you want to activate.

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

Unique title: "Implementing Unique Event Handlers with VueJS Components"

My VueJS and Buefy project begins with two distinct click actions: Click on the Cyan area -> redirects to another page (Action 1) Click on the Magenta area -> shows a dropdown menu (Action 2) https://i.stack.imgur.com/AVLOS.png However, when clic ...

Apply CSS styles when clicked and remove them when clicked again

I have successfully set a style when clicking on a div. However, I am looking to reset the style to default when clicked again. $('#tray-button').click(function() { $('.buttons-content').css('bottom', '160px& ...

Error Message: Initial Connection Failed - Unable to Establish Connection with MongoDB Server

After trying to execute a GET request from my mlab database, I encountered the following issue: (node:47578) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to establish a connection with the server [ds157439.mlab.com:57439] on the initial atte ...

Developing a PL/SQL function based on a query

Looking for assistance with transforming a PL/SQL query into a function named reject_list. Here is what I have attempted so far: create or replace function reject_list(ayrc in varchar2, mcrc in varchar2) return string begin select distinct '<tr> ...

Utilize jQuery to target elements that do not contain any children or whose first child is not an

I have been experimenting with dynamically styling elements on my web pages. Here is an example of the code I am working with: <a href="#"> Select Me </a> <a href="#"> <img src="blah.jpg" /> Don't select me </a> ...

What is the best way to obscure a potential phone number within a string of text?

Utilizing Google Cloud DLP in node.js to censor phone numbers from a string, even if the string contains other words. Check out more information on how to use this feature here. For instance, I need to redact the phone number in the following sentence: Do ...

What exactly does original-title refer to in HTML coding?

I have been searching extensively, but I cannot find any information that explains the meaning and proper usage of the original-title in HTML code. I am unsure if original-title is a Tag or Attribute in HTML, and if it is the same as title? I came across ...

Experience the convenience of Visual Studio Code's auto-completion feature for HTML tags even when working within an

My HTML file has a babel script embedded within it. <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>React tutorial</title> <script src="https://unpkg.com/react@16/umd/react.development.js" ...

What is the process for creating a List Grid View utilizing this particular code?

I attempted to modify the stylesheet extensively and also created a small function in JavaScript, but unfortunately it did not yield the desired results. <div class="row featured portfolio-items"> <div class="item col-lg-5 col-md-12 col-xs ...

Next.js is able to generate a unique URL that strictly handles code execution without any visual elements

Currently, I am in the process of developing a new website using NextJS. One issue that has come up involves a password reset verification endpoint. After a user initiates a password reset, it is sent to the API for processing and then redirected back to ...

Implementing conditional ng-show based on checkbox status in AngularJS

I have created this code to filter out the out-of-stock products using ng-show. When the checkbox is checked, only the available products should be displayed. HTML: <input type="checkbox" id="chkStock" value="Exclude Out of Stock" ng-model="exclude" / ...

How to Use Discord.js v13 to Make an Announcement in a Specific Channel

Looking for a Solution I am trying to create a command that allows an admin user to send a specific message to a designated channel. The Issue at Hand My current approach involves sending a response back to the user who triggered the command with the ent ...

Revamp your search experience with Algolia's Angular Instant Search: Design a personalized search box template

Custom Search Box Request: My goal is to implement an autosuggest search box using Algolia Angular instant search with an Angular Material design. To achieve this, I am planning to customize the search box component by replacing the standard <ais-sea ...

Enhance input and icon alignment using CSS based on the Bootstrap framework

How can I create focus on two elements simultaneously? <div class="form-group"> <label class="control-label"></label> <div class="col-sm-10"> <input type="text" class="form-control"> <span class="lo ...

Errors from jQuery validation are causing disruptions in my form's functionality

When jQuery validation error messages appear, they take up space and push other fields downwards. I want the error messages to adjust within the available space and not disrupt other fields or elements. Below is the jQuery validation code I'm currentl ...

The object returned by bodyParser is not a string but a data structure

I have been working on implementing a JSON content listener in Typescript using Express and Body-parser. Everything is functioning perfectly, except when I receive the JSON webhook, it is logged as an object instead of a string. import express from 'e ...

issue with centering using margin auto

Below is the HTML and CSS snippet for reference: * { margin: 0; padding: 0; } html, body { height: 100%; width: 100%; } header { height: 100px; background-color: #35424a; ...

When I click on it, my div refuses to slide down

Help needed with creating a drop-down menu on my small website. Having trouble getting the desired slideDown effect to work, even though other animations are functioning fine. The text on the div reads "Menu". Below is the jQuery code: $(document).ready ...

Make the inner div's width and height set to 100% of the available space within another div

#outer-wrapper { background-color: #585858; width: 200px; height: 200px; } #inner-wrapper { background-color: #111858; width: 50px; height: 100%; } <div id="outer-wrapper"> content 1 <div id="inner-wrapper"></div> </div ...

Conceal a dropdown menu with a single click

I want the dropdown to disappear temporarily after I make a selection, but still be available for me to hover over later. This is my current code: <div class="navbutton_container"> <button onclick="someFunction()"><p ...