Problem with the visibility of Grid Layout

Trying to create a Quiz web application, I want the quiz container to be hidden with the CSS property display: none; until I click "start game." Once the game starts, the container should change to display: grid;. However, when I try to reveal the div, it doesn't appear. Any ideas?

const buttonGrid = document.querySelector('.grid');
function startGame() {
    buttonGrid.remove(); // Remove entire grid container
    logo.forEach(logoElement => logoElement.remove()); // Remove each logo element

    const selectedFile = fileInput.files[0];

    if (selectedFile) {
        const reader = new FileReader();

        reader.onload = function (e) {
            const fileContent = e.target.result;

            try {
                const quizData = JSON.parse(fileContent);
                displayQuiz(quizData);
            } catch (error) {
                console.error("Error parsing JSON file:", error);
            }
        };

        reader.readAsText(selectedFile);
    } else {
        console.error("No file selected.");
    }
    
    const quizField = document.getElementById('quiz-container');
    quizField.classList.add('show-container');

}
.container{
    background: white;
    height: 100vh;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center; /* Center vertically */
    align-items: center; /* Center horizontally */
    #question{
    font-size: 25px;
    margin-bottom: 150px;

}
.quizField{
    display: none;
    text-align: center;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    

}
#submit-btn{
    margin-top: 50px;
    display: inline-block;
    margin: 0 auto;
    
}
#result {
    margin-top: 20px;
}
#total-score{
    font-size: 18px;
    margin-top: 20px;
    font-weight: bold;
}
show-container{
    display: grid;
}
.button-56{
    align-items: center;
    background-color: rgb(74, 227, 36);
    border: 2px solid #111;
    border-radius: 8px;
    box-sizing: border-box;
    color: #111;
    cursor: pointer;
    display: flex;
    font-size: 16px;
    height: 48px;
    justify-content: center;
    line-height: 24px;
    max-width: 100%;
    padding: 0 25px;
    position: relative;
    text-align: center;
    text-decoration: none;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}
.button-56:after{
    background-color: lightgrey;
    border-radius: 8px;
    content: "";
    display: block;
    height: 48px;
    left: 0;
    width: 100%;
    position: absolute;
    top: -2px;
    transform: translate(8px, 8px);
    transition: transform .2s ease-out;
    z-index: -1;
}
.button-56:hover:after{
    transform: translate(0, 0);
}
  
.button-56:active{
    background-color: #ffdeda;
    outline: 0;
}
  
.button-56:hover{
    outline: 0;
}
<body>
    <!-- Main container for the page -->
    <div class="container">
        <!-- Question and Answer Fields-->
        <div id='quiz-container' class="quizField">
        <div id="question"></div>
        <input type="text" id="answer-input" placeholder="Your Answer">
        <button class='button-56' id="submit-btn" onclick="submitAnswer()">Submit</button>
        <div id="result"></div>
        <div id="total-score"></div>
    </div>
     <div class="grid">
            <button id="start" class="button-56" role="button">Start</button>
            <label  for='fileInput' id='list' class="button-56">Open JSON File </label>
                <input type="file" id="fileInput" class="file" accept=".json" /></button>
                 
            <button id='contact' class="button-56" role="button">Contact</button>
        </div>
        

Answer №1

Integrating the onclick="startGame()" function into the Start button, along with certain sections of JavaScript being commented out.

const buttonGrid = document.querySelector('.grid');
function startGame() {
    buttonGrid.remove(); // Eliminate the entire grid container
/*
    logo.forEach(logoElement => logoElement.remove()); // Delete each logo element
    const selectedFile = fileInput.files[0];

    if (selectedFile) {
        const reader = new FileReader();

        reader.onload = function (e) {
            const fileContent = e.target.result;

            try {
                const quizData = JSON.parse(fileContent);
                displayQuiz(quizData);
            } catch (error) {
                console.error("Error parsing JSON file:", error);
            }
        };

        reader.readAsText(selectedFile);
    } else {
        console.error("No file selected.");
    }
*/    
    const quizField = document.getElementById('quiz-container');
    quizField.classList.add('show-container');

}
.container {
    background: white;
    height: 100vh;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center; /* Vertically centered */
    align-items: center; /* Horizontally centered */
    #question{
    font-size: 25px;
    margin-bottom: 150px;
}

.quizField {
    display: none;
    text-align: center;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
}

#submit-btn {
    margin-top: 50px;
    display: inline-block;
    margin: 0 auto;
}

#result {
    margin-top: 20px;
}

#total-score {
    font-size: 18px;
    margin-top: 20px;
    font-weight: bold;
}
.show-container{
    display: grid;
}
.button-56{
    align-items: center;
    background-color: rgb(74, 227, 36);
    border: 2px solid #111;
    border-radius: 8px;
    box-sizing: border-box;
    color: #111;
    cursor: pointer;
    display: flex;
    font-size: 16px;
    height: 48px;
    justify-content: center;
    line-height: 24px;
    max-width: 100%;
    padding: 0 25px;
    position: relative;
    text-align: center;
    text-decoration: none;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}
.button-56:after{
    background-color: lightgrey;
    border-radius: 8px;
    content: "";
    display: block;
    height: 48px;
    left: 0;
    width: 100%;
    position: absolute;
    top: -2px;
    transform: translate(8px, 8px);
    transition: transform .2s ease-out;
    z-index: -1;
}
.button-56:hover:after{
    transform: translate(0, 0);
}
  
.button-56:active{
    background-color: #ffdeda;
    outline: 0;
}
  
.button-56:hover{
    outline: 0;
}
<body>
    <!-- Main container for the page -->
    <div class="container">
        <!-- Question and Answer Fields-->
        <div id='quiz-container' class="quizField">
        <div id="question"></div>
        <input type="text" id="answer-input" placeholder="Your Answer">
        <button class='button-56' id="submit-btn" onclick="submitAnswer()">Submit</button>
        <div id="result"></div>
        <div id="total-score"></div>
    </div>
     <div class="grid">
            <button id="start" class="button-56" role="button" onclick="startGame()" >Start</button>
            <label  for='fileInput' id='list' class="button-56">Open JSON File </label>
                <input type="file" id="fileInput" class="file" accept=".json" /></button>
                 
            <button id='contact' class="button-56" role="button">Contact</button>
        </div>

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

Creating a right-aligned glyph within a Panel header using Bootstrap

I am currently designing a Panel using bootstrap and I would like to include a glyph in the heading, aligned to the right. I tried adding "text-right" to the span tag but unfortunately, it didn't work as expected. Here is the code snippet: < ...

What is the method for sending parameters to PHP from an HTML file using AJAX?

My protfolio.html file contains a table #gallery with different categories. I want to dynamically update the content of the #gallery based on the selected category using ajax. I have a php file that scans a specific folder for images related to the categor ...

Real-time audio feed permanently stuck in Chromium-powered web browsers

Here is how the audio element appears. No interaction seems to take place with it. (Experimented on ungoogled chromium, Edge, and an android device using a default Chrome browser) Below is the HTML code for the audio element: <div> <audio co ...

Using a variable as a DOM object in scripts: A simple guide

I am facing a challenge with organizing multiple instances of functions and making them easily reusable by streamlining them. To prevent hard coding values, I am working on setting up a parent ID to execute more comprehensive loops. http://jsfiddle.net/h ...

Display or conceal a frame with the help of JavaScript or jQuery

Is there a simple way to hide the "topFrame" and "menu" frames when clicking an image or button, and then unhide them by clicking again? Apologies for not including the "topFrame" and "menu" jsp files, as they are dynamically generated in my application. ...

Update the CURLOPT_URL parameter to accept user input

As someone new to coding, I have a question about using the CURLOPT_URL function in PHP. How can I input a NIK number and retrieve data from this URL: ? Any help would be appreciated, thank you! php $curl = curl_init(); curl_setopt_array($curl, [ CU ...

Utilizing window.matchMedia in Javascript to retain user selections during page transitions

I am encountering difficulties with the prefers-color-scheme feature and the logic I am attempting to implement. Specifically, I have a toggle on my website that allows users to override their preferred color scheme (black or light mode). However, I am fac ...

Utilize Redux in conjunction with TypeScript to seamlessly incorporate a logout feature

My login page redirects to a private /panel page upon successful login with an accessToken. I am utilizing the Redux store to verify the token in the privateRoute component. Challenges I'm encountering: I aim to enable logout functionality from t ...

All I desire is to eliminate the term "class" from the text

Upon clicking the code, only the value 137 should display according to the image. However, the word "class" also appears. https://i.stack.imgur.com/HZhr4.png <a rel="facebox" href="portal.php?id='.$rowa["_id"].' class="icon-remove"></a ...

Displaying incorrect symbols with icon fonts

I have successfully integrated icon fonts into my simple web page. Check it out on jsFiddle here My design is similar to this example, but instead of bars, I see a snake icon displayed. How can I fix this issue with the fonts? Here is the code snippet: ...

Discover the art of utilizing two distinct binding strings, wherein upon the selection of either, the alternate binding string shall automatically be

Having to use two different bindingstrings is a requirement due to a tool used for creating PDFs. My objective is to have the corresponding bindingstring turn "Off" when a user clicks on either the Yes or No button, and have the clicked button turn to "Yes ...

Error encountered while parsing data in Internet Explorer versions 7, 8, 9, and 10 due to an invalid character. The status

This block of code is functioning correctly on Chrome and Firefox, however it seems to be having issues with Internet Explorer! It involves a simple JSON file call, fetching data, and then displaying it on an HTML webpage. Here's the code snippet: $. ...

The reduce function doesn't return a value in JavaScript

const items = [ { item: 'apple', cost: 2 }, { item: 'orange', cost: 4 }, { item: 'pear', cost: ' ' }, { item: 'grape', cost: 6 }, { item: 'watermelon', cost: 8 }, { item: 'kiwi', cost: & ...

What is the best way to apply styles based on specific screen widths while still having default styles for other screen sizes?

Here is the code snippet I am working with: <TableCell sx={{ borderBottom: { xs: 0, lg: 1 } }}> <Typography variant="body2">{account.name} ({account.currency})</Typography> </TableCell> I am facing an issue where the ...

JavaScript validation code for verifying hostnames with a specified domain name

I need a customized validation check for my order form where users are asked to enter their server hostname. The challenge is that I'm using WHMCS with encrypted PHP code and IonCube loaders, making it difficult to add this check. Users can input any ...

Stop the Bootstrap row from automatically adjusting to the height of the tallest child column element, and instead utilize the extra space below the smaller element

My current framework is Bootstrap 5.2. I am aiming to create two columns, each with a col-md-6 class inside a row. You can visualize the layout through this image: . The challenge I'm facing is the inability to add another row with col-md-6 element ...

Having difficulty accessing the attributes of an object within a property

I am encountering an issue when trying to access the properties of an object that contains a reference property of another object. Essentially, there is an object nested within another object. Upon fetching data from API calls like this one for instance:, ...

Transforming an interactive HTML webpage into React/JSX

Imagine a scenario where I have two files: example.html <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="u ...

How can a function be executed asynchronously in node.js?

What is the best way to run a function in the background using node.js? For example: task = function(){ for (var i=0; i<1000000; ++i); return "world!"; }; background(task).then(console.log); console.log("Hello "); You should see the expected ...

The battle between Iteration and Recursion: Determining the position of a point in a sequence based

One of the challenges I'm facing involves a recursive function that takes a point labeled {x,y} and then calculates the next point in the sequence, recursively. The function in question has the following structure: var DECAY = 0.75; var LENGTH = 150 ...