Changes to CSS rules were implemented using Javascript, causing visible effects in Chrome but not in Firefox or Internet Explorer

My website features a unique toggle function to show and hide divs, activated by clicking a button. While this functionality is flawless in Chrome, other browsers are not cooperating.

The issue lies with my JavaScript code that adds an event listener for each button click. Upon clicking a button, the function changes the className of the 3 div elements. Depending on the button clicked, the divs are assigned either the invisible or visible class.

In testing, I have run a javascript snippet alert(left_box.className); which correctly gives alternating alerts of content-box visible & content-box invisible. So it appears that the rules are being modified. However, these CSS rule adjustments are not reflecting in Firefox and IE.

Help!

var button_1 = document.getElementById('button-1');
var button_2 = document.getElementById('button-2');
var button_3 = document.getElementById('button-3');


var left_box = document.getElementById('left-box');
var center_box = document.getElementById('center-box');
var right_box = document.getElementById('right-box');


function one_clicked() {
    
    if (left_box.className == "content-box visible") {
        left_box.className = "content-box invisible"}
    else {
    
        left_box.className = "content-box visible";
    
        center_box.className = "content-box invisible";
        right_box.className = "content-box invisible";
        }
    alert(left_box.className);
};

function two_clicked() {
    
    if (center_box.className == "content-box visible") {
        center_box.className = "content-box invisible"}
    else {
        center_box.className = "content-box visible";
    
        left_box.className = "content-box invisible";
        right_box.className = "content-box invisible";
    }
    alert('hello');
};

function three_clicked() {
    
    if (right_box.className == "content-box visible") {
        right_box.className = "content-box invisible"
    }
    else {
    right_box.className = "content-box visible";
    
    center_box.className = "content-box invisible";
    left_box.className = "content-box invisible";
        }
    alert('fuckkkkkk');
};

button_1.addEventListener('click',one_clicked, false);
button_2.addEventListener('click',two_clicked, false);
button_3.addEventListener('click',three_clicked, false);
body {
    margin:0px;
    background-color:cyan;
    padding:3%;
}

.main-box {
    background-color:lightblue;
    height:75%;
    
}

.main-row {
    margin-top:3%;
    margin-bottom:3%;
    display:flex;
    flex-direction:row;
    justify-content:space-between;
    
}

button {
    height:7%;
    width:25%;
    
}

.content-container{
    display:flex;
    justify-content:space-between;
}
.content-box {
    width:25%;
    height:80%;
    background-color:pink;
    border-radius:10%;
}


.visible {
    height:180%;
    width:150%;
}

.invisible {
    height:0.1px;
    background-color:transparent;
}


.footer{
    margin:0px;
}
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="expansion.css"></link>
        <title>Expansion</title>
        <meta charset="utf-8"/>
    </head>
    
    <body>
        
        <div class="main-box"></div>
        
        <div class="main-row">
            <button id="button-1">1</button>
    
            <button id="button-2">2</button>
            
            <button id="button-3">3</button>
        
        </div>
        
        <div class="content-container">
            
        
            <div class="content-box visible" id="left-box"></div>

            <div class="content-box invisible" id="center-box"></div>

            <div class="content-box invisible" id="right-box"></div>      
        
        </div>
        
        
<!--
        
        <footer>Footer</footer>
-->
     <script src="expansion.js"></script>
                

    </body>
</html>

Answer №1

Your code is functioning correctly as expected. The div appears collapsed because it does not contain any content inside. Once a span is added and the CSS property changed to display, the content displays as intended.

var button_1 = document.getElementById('button-1');
var button_2 = document.getElementById('button-2');
var button_3 = document.getElementById('button-3');


var left_box = document.getElementById('left-box');
var center_box = document.getElementById('center-box');
var right_box = document.getElementById('right-box');


function one_clicked() {

  if (left_box.className == "content-box visible") {
    left_box.className = "content-box invisible"
  } else {

    left_box.className = "content-box visible";

    center_box.className = "content-box invisible";
    right_box.className = "content-box invisible";
  }
  console.log('Left Box');
};

// More functions for two_clicked and three_clicked...

button_1.addEventListener('click', one_clicked, false);
button_2.addEventListener('click', two_clicked, false);
button_3.addEventListener('click', three_clicked, false);
body {
  margin: 0px;
  background-color: coral;
  padding: 3%;
}
.main-box {
  background-color: cornflowerblue;
  height: 75%;
}

// More CSS styles...

.footer {
  margin: 0px;
}
<html>

<head>
  <link rel="stylesheet" type="text/css" href="expansion.css"></link>
  <title>Expansion</title>
  <meta charset="utf-8" />
</head>

<body>

  <div class="main-box"></div>

 // Rest of the HTML structure...

  <script src="expansion.js"></script>


</body>

</html>

It's recommended to use console.log instead of alert.

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

Issue with Bootstrap.js causing mobile menu to not expand when toggled in Rails app

Despite adding the .toggled class, my hamburger menu is not expanding when I click on the mobile menu. I've tried adjusting the order of the required javascript commands, but it doesn't seem to be working as expected. Here are the steps I'v ...

Creating multiple columns in a single row using Bootstrap5 for front-end development

Is there a way to make 10 columns responsive in the same line with Bootstrap 5? I noticed that after 5 or 6 columns, they automatically move to the next line. Using a class like "col-1" seems to disrupt the responsiveness and the columns don't fully ...

Caution: It is important for every child in a list to have a distinctive "key" prop value. How can a shared key be used for multiple items?

When I click on a header from my list, an image should appear. However, I'm encountering an error that needs to be resolved. I've been struggling to find a solution for adding unique keys to multiple elements in order to eliminate the error. Des ...

DirectionsLight isn't functioning properly in Three.js

I attempted to add lighting in Three.js, but after updating the display, the light didn't seem to affect my cylinder. I simply copied and pasted the source code from the Three.js documentation Three.js as instructed, but to no avail. Below is the cod ...

Is the use of htmlspecialchars() in jquery .html() function sufficient to prevent javascript attacks when retrieving data through ajax requests?

I have developed a PHP and jQuery-AJAX script that performs well. However, I have noticed that it runs slowly and is vulnerable to XSS attacks. Upon researching further, I came across some relevant information in the first answer of this link: please see t ...

Using Node.js Express for the backend and React.js for the frontend

After setting up my server code in Node JS Express and receiving my response in JSON form, I followed a tutorial to create the project structure. However, now I want to build the front end of the application using React. As a beginner in React, I am lookin ...

Looking to generate a computed attribute?

Trying to adjust the font size of text using a dropdown and so far it's working as expected. However, is there a more efficient way to achieve this, such as using a computed property or watcher? I'm not sure how to go about implementing that. He ...

Associating specific information with individual elements utilizing HTML text box

Seeking guidance on implementing annotations feature for a scatter-plot using d3.js v3. My goal is to show a text-box upon clicking each data point, then display that entered text as a tool-tip specific to that data point. Here's how I'm approach ...

Exploring the possibilities of applying a fragmentShader texture to repeat a ThreeJS Matcap

Here are the vertex and fragment shader materials: material = new THREE.ShaderMaterial( { uniforms: { textureMap: { type: 't', value: THREE.ImageUtils.loadTexture( 'img/matcap/green.jpg' ) }, normalMap: { type: 't' ...

I'm currently utilizing the react mui package, specifically @mui/x-date-pickers. Could someone kindly provide guidance on how to customize the color of the

I am currently working with MUI in React and using the DatePicker component from the @mui/x-date-pickers library. The version I am using is 6.0.3. I have encountered an issue where, upon selecting the first day, the specified color changes as shown in the ...

In ReactJS, one can create a variable and include a map function by first declaring the variable and then using the map function within the component. If you

Having trouble integrating the accordian.js page into the app.js main page for compilation. Need help defining and writing out the function correctly, any suggestions? Below is my code: App.js: How do I incorporate the components from the accordian.js pa ...

Having an excessive amount of CSS Sprites or none at all can be detrimental to your website's performance

UPDATE: Attempted to adjust the height to 27px as recommended by kennypu, but still facing issues. I am working with a sprite containing multiple icons that I want to extract for navigation purposes. Unfortunately, I'm struggling to properly display ...

Exploring the world of digital audio files and understanding the distinction between 'file:/// and '

I'm experiencing an issue with playing MP3 and MP4 files in Firefox and Internet Explorer. Can someone explain the difference between running a page by double-clicking on it (file:///...) compared to using IIS (http://...)? <object data="../../Med ...

The guildMemberAdd event seems to be malfunctioning within my discord.js bot

I need help solving a problem and figuring out how to correct this error. I've spent around 3 hours trying to fix it, but I haven't made any progress. When someone joins the server, I want an event to trigger that lists their nickname, tag, and ...

Strategies for Implementing Multi-Step Password Form Validation

Currently, I am using https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_form_steps as the foundation of my form with some adjustments. Validation is functioning correctly where empty fields disable the next button. However, when I attempt to add ...

What is the best method for converting a string to an integer when transitioning from CSV to JSON format?

Upon discovering this code snippet designed to convert a CSV file into JSON format, I encountered a specific requirement. In this scenario, the credit field needs to be an integer, which means it should not be enclosed within quotation marks like "". I de ...

Using Javascript, retrieve a custom attribute specific to a checkbox option

How can I access the custom attribute "mem_name" value? <input class="messageCheckbox" type="checkbox" value="3" mem_name='ABC' name="checkmember" > <input class="messageCheckbox" type="checkbox" value="1" mem_name='PQR' name ...

There appears to be a malfunction in the socket rooms, as they are not operating

I have set up a code where sockets are being sent to the wrong person, and I am unable to figure out why. Whenever I update one user's status, it also updates the other user's status. Snippet of Code io.on('connection', function(socke ...

Tips for sending an Object within a multipart/form-data request in Angular without the need for converting it to a string

To successfully pass the object in a "multipart/form-data" request for downstream application (Java Spring) to receive it as a List of custom class objects, I am working on handling metadata objects that contain only key and value pairs. Within the Angula ...

Unable to establish a connection to a particular database and collection within the MongoDB Atlas Cluster

I am in the process of developing a MERN stack application and have opted to utilize mongoose for communication with MongoDB Atlas. However, I am facing difficulty in understanding how to connect to a specific database and collection within MongoDB Atlas d ...