The "Next" button fails to function after a replay has been completed

I created a small quiz app to practice my JS skills. Everything seems to be working fine, except for one issue - when I click on replay, the quiz box shows up but the 'Next' button stops functioning. There are no console errors and I'm struggling to pinpoint the problem in the code.

If you want to check out the working app, here is the jsFiddle link: https://jsfiddle.net/lifet/t20h6gw8/10/

The JavaScript file contains both the script and the questions for the quiz. Below is the HTML markup for the quiz and result box:

 <!--Start Quiz Box-->
       <div class="row quiz_box">
        <div class="col-md-6 col-sm-12">
            <div class="card">
                       //other sections of code
                    <footer>
                        <button class="next_btn">Next</button>
                       
                    </footer>
                </div>
            </div>
        </div>
       </div>         
        <!--End Quiz Box-->

        <!--Start Result Box-->
        <div class="row result_box">
            <div class="card col-md-6 col-sm-12">
            //other sections of code
            <div class="buttons">
                <button class="restart"&qt;Replay Quiz</button> 
            </div>
        </div>
        </div>
        <!--End Result Box-->
    </div>

Here is the CSS used to hide or show the result and quiz box accordingly:

.removeResultBox.result_box{
  display: none;
}
.removeResultBox.quiz_box{
  display: flex;
}

This is the JavaScript function for the replay option and next button click event:

//----------------  restart Quiz-------------------//

const replayQuiz = resultBox.querySelector(".restart");

function showQuizBox(){}

replayQuiz.onclick=() =>{
    quizBox.classList.add("removeResultBox");// this displays the quiz box
    resultBox.classList.add("removeResultBox");// this hides the result box

    queCounter = 0;
    queNumber = 1;
    startTime = 15;
    widthValue = 0;
    userScore =0;

   showQuestions(queCounter);
   questionCounter(queNumber);
   clearInterval(timeCounter);
   clearInterval(counterLine);
   startTimer(startTime);
   startTimeLine(widthValue);

   nextBtn.style.display="none";

}

And finally, the onclick event for the next button:

//go on to the next question
nextBtn.onclick=()=>{
    console.log("next");
    if (queCounter < questions.length - 1) {
        queCounter++;
        queNumber++;
        showQuestions(queCounter);
        questionCounter(queNumber);
        clearInterval(timeCounter);
        startTimer(startTime);
        clearInterval(counterLine);
        startTimeLine(widthValue)
        nextBtn.style.display="none";
    }
    else {
        showResultBox();
    }
}

If anyone can offer assistance with this issue, I would greatly appreciate it. Thank you!

Answer №1

It is essential to modify the sentence

const resetQuiz = document.querySelector(".restart"); refer to the online resource

Answer №2

When you click on the replayQuiz button, make sure to remove the showResultBox instead of adding the removeResultBox:

replayQuiz.onclick = () => {
  resultBox.classList.remove('showResultBox')
  quizBox.classList.remove('showResultBox')
  ...
}

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

How can I use XPATH to target a div after choosing a nested element?

I had the task of creating a universal identifier to locate a dropdown menu following a label. There are 10 different forms, each with a unique structure but consistent format, which means I cannot rely on specific IDs or names. Instead, my approach was to ...

Excessive JSON formatting is consuming an excessive amount of storage space

As I work on developing a website that recommends locations to visit in NYC, I am facing an issue with saving JSON data in local storage. My goal is to allow users to add their own suggestions and eventually integrate MongoDB into the site. To build the si ...

Unchecked checkbox displays as checked in UI

I am facing an issue with checking checkboxes using JavaScript. Even though the checkbox appears to be checked in the program, it does not reflect the updates on the user interface. If anyone has a solution for this, please share. <!DOCTYPE html> ...

Customizing error styles in a table using Jquery validation

My form is using JQuery .validation(). Here is the structure of my form: <form....> <table cellspacing="0" cellpadding="0"> <tr> <td>Name: </td> <td><input type='text' name='Name'/></td> ...

What is the best way to share models across different node.js projects?

In my setup, I have two node.js projects - project A and project B. Project A serves as the main project, while project B is more of an "ad-hoc" project with a specific purpose. The challenge lies in the fact that project B requires access to project A&apo ...

Steps for transferring JSON data from the controller to JavaScript

Within my cluster Table, there is a column called description which stores cluster coordinates in JSON format. I want to draw multiple cluster polygons on Google Maps using these coordinates. id | description ...

Error: The hyperlink in the response from the Ajax request is not functioning as expected

I've exhausted all the suggestions for fixing this issue, but nothing has worked so far. Currently, my setup involves making an AJAX call to a PHP page called livesearch.php from the index page in order to retrieve live search results. <html> ...

What is the best way to showcase all tab content within a single tab menu labeled "ALL"?

I have created a tab menu example below. When you click on the button ALL, it will display all the tabcontent. Each tab content has its own tab menu. Thanks in advance! function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = doc ...

Guide to initiating a node.js socket.io server through a brackets extension

I am currently working on developing a brackets extension that involves sending data to a server. What I aim to do is execute a server.js file from my main.js file, which will create a node.js socket.io server. Once this server is set up, the extension sho ...

Is it considered good practice to utilize Vue.js for managing global shared state and implementing for loops outside of components?

Just getting started with Vue.JS and experimenting with creating two lists of books (read and unread) from a shared object in the global data state. This is my approach - working demo (works like a charm! However, I'm wondering if this is considered ...

What is the best way to include a background image in a div within a React component?

I'm currently following a tutorial on creating an RPG game using React. The tutorial can be found at this link. I am trying to replicate the presenter's code by typing it out myself. However, I'm running into an issue when attempting to add ...

Learn how to incorporate additional rows into a table by pressing the plus button within the table with the help of Angular

I require some assistance. I am looking to dynamically generate a row after clicking on the plus button using angular.js. The newly created row should contain an ID and model generated dynamically. Here is an overview of my code: <table class="table ta ...

Tips for preventing a page refresh using HTML, JQuery, AJAX, and PHP

I need assistance with transferring the value of a selected radio button to a PHP session variable using Javascript/AJAX. Everything seems to be working fine, except that the page refreshes each time. Is there a way to prevent this from happening? Below i ...

Include a stylesheet as a prop when rendering a functional component

Using Next.js, React, Styled JSX, and Postcss, I encountered an issue while trying to style an imported component for a specific page. Since the stylesheets are generated for a specific component at render time, I attempted to include custom styles for the ...

Replicating a tag and inputting the field content

Scenario: An issue arises with copying a label text and input field as one unit, instead of just the text. Solution Needed: Develop a method to copy both the text and the input field simultaneously by selecting the entire line. Challenge Encountered: Pre ...

What is the optimal method for creating and testing AJAX applications on a local server, then effortlessly deploying them online?

Exploring AJAX development is new to me. The challenge I've encountered so far is dealing with the same-origin policy, which requires modifying host information strings like absolute URLs in JavaScript files every time I deploy local files to remote s ...

Why is the jQuery change event only firing when the page loads?

I am experiencing an issue with a .js file. The change event is only triggering when the page loads, rather than when the selection changes as expected. $(document).ready(function(){ $("#dropdown").on("change keyup", colorizeSelect()).change(); }); f ...

Alternative to using the disabled attribute in JavaScript to make a checkbox read-only option

Does anyone know how to make a checkbox readonly so that its value can be submitted, while also disabling it? Using the disable attribute prevents the value from being submitted, and setting it as readonly doesn't seem to work for checkboxes. Your as ...

Discover the Phillips Hue Bridge within the operational web application on a separate network

Utilizing the node-hue-api package on a Node.js/Express server to interact with the Hue API, I've developed an admin section of a website exclusively accessible to me for controlling my Hue lights. The functionality works seamlessly in my local develo ...

Sending a batch of items through an ajax request

I am faced with a challenge where I have a collection of data stored within multiple objects. Within each object, there is an ID and a status, while the main object contains a type and form id. The issue arises when trying to post the result via ajax as ...