Missing button element in HTML

I've been tackling a challenge while developing a quiz application. My aim is to display a button upon clicking the check button, but for some reason, it's not functioning as expected.

var pos = 0,
  test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
  ["What is the value of num?", "6", "5", "Potato", "A", "A Variable is something that can store data, like a number or a String (Some text). They can be written as an int or as a string. EXAMPLE: ", "https://s1.postimg.org/nqvwnr0un/Untitled.png"],
  ["What is 7 x 3?", "21", "24", "25", "A"],
  ["What is 8 / 2?", "10", "2", "4", "C"]
];

function _(x) {
  return document.getElementById(x);
}

function renderQuestion() {


  test = _("test_q");
  text = _("test_t");
  if (pos >= questions.length) {
    test.innerHTML = "<h2>You got " + correct + " of " + questions.length + " questions correct</h2>";
    _("test_status").innerHTML = "Test Completed";
    pos = 0;
    correct = 0;

    return false;
  }

  var BarPercent = (100 / questions.length) * (pos + 1);
  var pb = document.getElementById("pb");
  pb.style.width = BarPercent + "%";
  var qresult = false;

  function showResult(correct) {
    if (correct === true) {
      document.getElementById("Result").style.visibility = "visible";
    }
  }

  question = questions[pos][0];
  chA = questions[pos][1];
  chB = questions[pos][2];
  chC = questions[pos][3];
  info = questions[pos][5];

  if (questions[pos][6] !== undefined) {
    img = questions[pos][6];
  } else {
    img = ""
  }

  test.innerHTML = "<h4 style='color: #DDD;'>" + info + "</h3><br><img src='" + img + "' style='align: center; width: 50%;'>";
  test.innerHTML += "<h3>" + question + "</h3>";
  test.innerHTML += "<input type='radio' name='choices' value='A'> " + chA + "<br>";
  test.innerHTML += "<input type='radio' name='choices' value='B'> " + chB + "<br>";
  test.innerHTML += "<input type='radio' name='choices' value='C'> " + chC + "<br><br>";
  test.innerHTML += "<button id='sub_button'style='color: green; background-color: #CCC; border: 0px; width: 100%; text-align: left; font-weight: 100px; font-size: 70px; border-radius: 20px;' onclick='checkAnswer()'>Check</button>";

}

function checkAnswer() {
  choices = document.getElementsByName("choices");
  for (var i = 0; i < choices.length; i++) {
    if (choices[i].checked) {
      choice = choices[i].value;
    }
  }
  if (choice == questions[pos][4]) {
    correct++;
    qresult = true;
    showResult(qresult);

  }
  pos++;
  renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
div {
  border-radius: 20px;
}

#test_q {
  background-color: #AAA;
  padding: 10px 40px 40px 40px;
}

body {
  font-family: sans-serif;
  background-color: AAA;
  color: #EEE;
}

#pbc {
  width: 100%;
  height: 16px;
  background: #444;
  border-radius: 10px;
  margin-bottom: 20px;
}

#pbc>#pb {
  position: relative;
  top: 0px;
  background: #1D4;
  width: 0%;
  height: 16px;
  color: #0FF;
  text-align: center;
  border-radius: 10px;
}

#Result {
  margin-top: 150px;
  width: 100%;
  height: 100px;
  max-height: 100px;
  background-color: lightgreen;
  position: absolute;
  margin-bottom: 25px;
  border-radius: 20px;
  border: 0px;
  visibility: hidden;
}

#result {
  color: green;
  text-align: left;
  margin-left: 20px;
}
<div id="pbc">
  <div id="pb"></div>
</div>
<div id="test_q"></div>
<button id="Result" onclick="renderQuestion()">
  <h1 id="result"></h1>
</button>

Answer №1

I have made some modifications to the code you provided, give this a try:

   
var pos = 0,
       test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
     var questions = [
       ["What is num equal to?", "6", "5", "Potato", "A", "A Variable is something that can store data, like a number or a String (Some text). They can be written as an int or as a string. EXAMPLE: ", "https://s1.postimg.org/nqvwnr0un/Untitled.png"],
       ["What is 7 x 3?", "21", "24", "25", "A"],
       ["What is 8 / 2?", "10", "2", "4", "C"]
     ];

     function _(x) {
       return document.getElementById(x);
     }

     function renderQuestion() {
       // Code for rendering questions and handling answers
     }

     function showResult(correct) {
         // Function to display result based on correctness of answer
     }

     function checkAnswer() {
       // Function to check chosen answer against correct answer
     }

     window.addEventListener("load", renderQuestion, false);
    
    // CSS styling for elements in the quiz
    div {
      border-radius: 20px;
    }
 
    body {
      font-family: sans-serif;
      background-color: AAA;
      color: #EEE;
    }

    // More CSS rules for styling
    
 
<div id="pbc">
  <div id="pb">
  </div>
</div>
<div id="test_q"></div>
<button id="Result" onclick="renderQuestion()">
  <h1 id="result"></h1>
</button>

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

Is there a way to position the button at the bottom right corner of my div box?

Having trouble positioning the button in the bottom-right corner of the parent div? I attempted using float: right;, but it ended up outside of the box. How can I keep the button within the box and align it to the bottom-right corner? If you have a solutio ...

interactive symbols in a reorganizable tree structure

I have been developing a customizable tree list for our users to arrange their website content. It allows them to add and rearrange pages by dragging and dropping. Each list item includes the page name and three icons (lock, visible, and edit). The challe ...

Converting a Javascript array to an NSArray in Xcode

As a complete beginner to Xcode and programming in general, I recently built an app using javascript and html and integrated it into Xcode. Currently, my main focus is on extracting multiple arrays from the html/javascript file and exporting them as a csv ...

Why does the code require the use of window when the Javascript error object is not functioning properly?

I'm struggling to grasp the distinction between var maxResult = window.max(maxValInt1, maxValInt2); which functions properly, and var maxResult = max(maxValInt1, maxValInt2); which gives an error "object is not a function". Why do I have to inclu ...

Enhancing Your Product List with PrestaShop Print Capabilities

I am having an issue with printing specific features from a product list: {if isset($product.features)} <div class="features"> {foreach from=$product.features item=feature key=key} {if $key < 2} <table ...

Is it possible that the passing of Form Serialize and list to the controller is not functioning properly?

i am struggling with the following code in my controller: public ActionResult SaveWorkOrder(DTO_WorkOrder objWork, List<DTO_PartsWO> listTry) { //something } here is my model : public class DTO_WorkOrder { public string Id { get; set; ...

Styling Your Website: Embrace CSS or Stick with Tables?

My goal is to create a layout with the following features, as shown in the screenshot: The main features include: The screen is divided into 3 regions (columns); The left and right columns have fixed widths; The middle column expands based on the browse ...

"Standard" approach for a Date instance

During my time in the Node REPL environment, the following output was displayed: > console.log(new Date()) 2023-08-15T09:21:45.762Z undefined > console.log(new Date().toString()) Sun Aug 15 2023 09:21:50 GMT+0000 (Coordinated Universal Time) undefine ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

Retrieving data efficiently: utilizing caching in an AngularJS service to store data fetched from a single $http request

Forgive me if this question seems simple or silly, but I find myself in a new scenario and need some guidance. In my AngularJS app, I have a service with 4 methods that share about 80% of the same functionality and code. I want to optimize this and make it ...

Show a Popup Modal after Submitting a GET Request

How can I display the SQL code returned from various input parameters as a Modal? I am struggling to have the POST request made from inputs show up in the Modal when pressing the submit button. I know Ajax is usually involved in this process, but how do I ...

Is it possible to apply fog to a specific area in Three.js instead of being dependent on the distance from

Picture this: a vast THREE.PlaneGeometry representing the floor with a camera placed at an arbitrary spot within the scene. By manually adjusting the near and far values of the fog, I can effectively conceal the outer edges of the plane to create the illu ...

Image that floats or pops over the content

I'm currently working on designing a card similar to the one displayed in the image. However, I'm facing difficulty in creating the floating image effect on the card. Any tips or suggestions on how to achieve this using angular, jquery, css, or a ...

Error with infiniteCarousel in Internet Explorer versions 7 and 8 when using jQuery

Hello everyone! I've run into a little issue with some jQuery code I'm using. It was working fine before, but after making several changes and improvements, I can't seem to figure out what the problem is. I keep getting JS errors in both IE7 ...

Retrieve data attributes to obtain details about the slider before and after

My task is to create a slider with information about the previous and next slides. For example, in the slider, there are left < and right > arrows. Behind these arrows, there are circles. When you hover over any arrow to change the next or previous ...

Conceal the standard style class of the p:selectOneRadio element

On my xhtml page, I've implemented p:selectOneRadio. However, I'm struggling to remove the default style class .ui-helper-hidden-accessible, resulting in the radio button icons not being visible. Here's a snippet of my code: <p:selectOne ...

The menu bar ought to transition from the left side to the top on smaller screens

Currently, I am in the process of creating a technical documentation project through freecodecamp. Here is the code that I have been working on: #main-doc { margin-left: 270px; } .heading { font-size: 1.7rem; font-family: Verdana, Geneva, ...

Is there a way to connect CSS files from a different directory?

I'm currently in the process of developing a website and I encountered an issue with linking my CSS file, which is stored in a different folder. I originally had it in the same directory as my HTML code, but now I need to figure out how to link it pro ...

Adding a variable to the .append function in HTML code

I am currently working on a way to include the current date and time when a user comments on a post in my forum. While I have successfully managed to upload the text inputted by the user into the comment section, I am now looking to also dynamically insert ...

How can I stop popup labels from appearing in MapBox GL JS when I don't want them to be visible?

Having created an application using MapBox GL JS, I have placed numerous markers all around the globe. As the mouse hovers over these markers, a description box pops up, which is what I intended. However, I am encountering an issue where these labels flick ...