Develop an Engaging JavaScript Quiz with Elements That are Generated Dynamically

**

Need help setting answer values for checkboxes

Currently, I have a code that displays (2) questions with (4) possible answers each. Next to each answer is a checkbox with a default value of false. However, I want to link the value of each checkbox to its corresponding answer.

Any suggestions on how to achieve this?

**

JavaScript-------------------------------------------------------------------

//Creating a JavaScript Quiz
//Array containing questions and their respective answers.
let quizBank = [
["Which object executes code continuously until an exit condition is met?", "Loop", "Array","Function","Object"],
["Where should we place JavaScript within HTML?","javascript",
"js","scripting","script"]
];

//Function to display questions, answers, and checkboxes based on array.
function printQuiz(quiz) {
let check = "<input type='checkbox' id='response' name='response' label='response'>";
    let listHTML = "<ol>"
    for(let i = 0; i < quiz.length; i++){
    listHTML += "<li>" + (i + 1) +": " + quiz[i][0] + "</li>" +
  "<li>" + check + quiz[i][1] + "</li>" +
  "<li>" + check + quiz[i][2] + "</li>" +
  "<li>" + check + quiz[i][3] + "</li>" + 
  "<li>" + check + quiz[i][4] + "</li>";
  }
  listHTML += "<ol>";
  return listHTML;
}
//Display the quiz using printQuiz function
document.getElementById("quiz").innerHTML = printQuiz(quizBank);

/* Accessing checkbox values */
/* Get the li elements under #quiz */
let listItems = document.getElementById("quiz").childNodes[0].childNodes;

/* Loop through li elements to find the input */
listItems.forEach(function(element) {

  if (element.childNodes.length > 1) {
   // The first child node within the li element is the checkbox input.
   let checkbox = element.childNodes[0];
   console.log(checkbox.checked);
    });

HTML-----------------------------------------------------------------------------

<div>
<h1>
      Quiz: Programming Concepts
</h1>
<div id="quiz"></div>
</div>

CSS------------------------------------------------------------------------------

li {
  display: block;
  margin: 1em;

}

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

Developing a personalized logging service in NestJs: capturing logs without directly outputting them

I am currently working on developing a NestJs service that will enhance a Logger. However, I am facing issues with achieving the desired output (e.g., super.warn(); see below for more details). I have followed a tutorial from the nestjs site, but unfortuna ...

Bizarre Object notation with JavaScript

While browsing through a Library called WebApp.net, I stumbled upon this interesting piece of code: var $h = { get HEAD() { return 0 }, get BACK() { return 1 }, get HOME() { return 2 }, get LEFT() { return 3 }, get RIGHT() { return 4 } ...

Display Error with Custom Alert Box

I recently implemented a customized alert box from slayeroffice's website, which you can find at slayeroffice.com/code/custom_alert/. When I view it on my browser, the alert box appears with a blue color in the center of the screen. Here is how it lo ...

Showcasing diverse content with an Angular Dropdown Menu

I'm currently developing an angular application, and I've encountered a difficulty in displaying the user's selection from a dropdown menu. To elaborate, when a user selects a state like Texas, I want to show information such as the period, ...

When using getStaticPaths, an error is thrown stating "TypeError: segment.replace is not a function."

I am a beginner when it comes to using Next.js's getStaticPaths and I recently encountered an error that has left me feeling lost. Below is the code I have written (using query from serverless-mysql): export async function getStaticPaths() { cons ...

I'm having trouble understanding why I am not receiving any response with the "form.submit()" function inside xhr.onreadystatechange() in JavaScript/AJAX

When I try to use the form.submit() function in response to xhr.onreadystatechange, it doesn't work as expected. Below is my login form: <form action="user_home.php" method="post" id="loginForm" > <tr> <td colspan=2>Members ...

What could be causing the Jquery keydown event to only trigger every other key press?

Recently, I have put together a thumbnail gallery that can be navigated using left and right arrows. I have noticed that while the right arrow works smoothly, the left arrow only seems to navigate to the previous thumbnail every other time. Can someone ple ...

VS Code sees JavaScript files as if they were Typescript

I have recently delved into the world of Typescript and have been using it for a few days now. Everything seems to be working smoothly - Emmet, linting, etc. However, I've encountered an issue when trying to open my older JavaScript projects in VS Cod ...

Display the query results on a separate blade

In my original attempt, I intended to display the results in a modal but later decided to show them in a different blade. My goal is to fetch all comments related to a post using its post_id. However, I encountered the following error: The GET method is no ...

Preventing links from functioning on dynamically generated web pages

I have implemented the following code to deactivate all links on a preview page: var disableLink = function(){ return false;}; $('a').bind('click', disableLink); While this successfully disables all static links, any anchor tags loade ...

Do I need to recompile anything after making changes to a node module in my vue.js project in order for the modifications to take effect? The updates are not appearing as expected

Currently tackling a Vue.js project and came across an issue with the Bootstrap-vue module that I would like to remove. After locating the section in the module responsible for this behavior, I commented it out. But strangely, the changes are not showing ...

Invoking a Referenced Array with a Function Call

Having trouble with a function call for an array and encountering the error: cannot refer to template array without an argument list repeatedly. How can I efficiently pass this array by reference to the main function? using namespace std; //function pr ...

React JS - State values are not persisting and rendering properly upon clicking

Recently, I followed a step-by-step tutorial on creating a todo list using functional components. However, I encountered an issue when attempting to delete or mark items as complete in the list. In both the deleteHandler and completeHandler functions, I tr ...

developing a stand-alone job listings feature

Our website features a job postings page that our clients are interested in incorporating into their own websites. This would allow applicants to easily apply for jobs directly on the client's site, with the information being saved in our system. One ...

Adding a fresh data point to Highcharts

Can the highchart line graph be updated every minute without starting from scratch? I want it to add new data points to the existing line, similar to the example shown in this jsfiddle. $(function () { $.getJSON('https://www.highcharts.com/sample ...

Determine changes in data retrieved from a JSON file using React

I've been working on a cryptocurrency app using React and a JSON API to fetch the latest data. My approach involves using fetch to load the JSON API and setInterval to refresh the app every 10 seconds. Now, I'm wondering if there's a way to ...

Creating a Vue2 modal within a v-for loop to display a dynamic

Recently, I've been working on implementing a vue2 modal following the instructions provided in the official Vue documentation at https://v2.vuejs.org/v2/examples/modal.html. The code snippet I have been using looks something like this: <tbody v-i ...

Facing issues with the forEach method in ReactJs when attempting to access data from an array

Struggling with a ReactJS issue, I've come across a problem with the HomePage Component where the forEach method isn't working as expected. I'm attempting to retrieve data from a defined array. Take a look at the code snippet below: import R ...

[Web Development]:

Having an issue with my HTML file where a JavaScript function named "CheckCaptcha" is not being executed when an image is clicked. I've been working on the code for hours, trying different tweaks and modifications, but it still can't seem to find ...

Mastering card sliding and spacing in React NativeLearn how to effortlessly slide cards horizontally in your React

My aim with the following code snippet is to achieve two objectives: Apply a margin of 20 units to each card Display all four cards in a single row, allowing users to swipe horizontally Unfortunately, I have not been successful in achieving either of th ...