The innerHTML of the p tag remains unaffected when using document.getElementsById("")

HTML

{% load static %}

 <link rel="stylesheet" type="text/css" href="{% static 'lessons/style.css' %}" />
<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "20%";
    document.getElementById("main").style.marginLeft = "20%";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "20%";
}
</script>


<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="{% url 'home' %}" class="active">Home</a>

<a href = "{% url 'lessons:index' %}"class="active">Lessons</a>

<a href="{% url 'resources' %}"class="active">Resources</a>

</div>

<div id="main">
  <span style="font-size:30px;cursor:crosshair;display:inline-block" onclick="openNav()">&#9776;</span>

<h1>Exercise</h1>
<form id = "exercise" action= " ">
{% for question in lesson.question_set.all %}
{% autoescape off %}{{ question.question_text }} {% endautoescape %}<BR>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% for choice in question.choice_set.all %}
  {% if choice.answer %}
<input type="radio" name="question{{question.pk}}" value=1 />{% autoescape off %}{{ choice.choice_text }}{% endautoescape %}<BR>
  {% else %}
<input type="radio" name="question{{question.pk}}" value=0 />{% autoescape off %}{{ choice.choice_text }}{% endautoescape %}<BR>
  {% endif %}
<!--<label for="{{question.pk}}choice{{ forloop.counter }}"></label><br />-->

{% endfor %}
<BR>
{% endfor %}<BR><BR>
<input type="submit" value="Submit"><BR><BR>
</form>
</div>

<center><p id="grade">Your grade is: </p></center>

<button class="accordion"></button>
<div class="panel">
  {% for question in lesson.question_set.all %}
  {% autoescape off %}{{question.answer}} {% endautoescape %}
  {% endfor %}
<BR><BR>
  <p align=center><a href="/../lessons/{{lesson.lesson_id}}" class="backbutton"><span><font-size=14px>Return to Lesson</font></span></a>
    <a href="/../lessons/quiz/{{lesson.lesson_id}}" class="button"><span><font-size=14px>Quiz {{lesson.lesson_id}}</font></span></a><BR><BR>
</div>

<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>

Javascript functions

  <script type='text/javascript'>

//start function
    <script>
  document.getElementById("exercise").onsubmit = function() {
  var result=0;
  { % for question in lesson.question_set.all % }
  result += parseInt(document.querySelector('input[name = "question{{question.pk}}"]:checked').value);
  { % endfor % }
  document.getElementById("grade").innerHTML = result;
  return false; // required to not refresh the page; just leave this here
  } //this ends the submit function
</script>
//end function






  var acc = document.getElementsByClassName("accordion");
  var i;

  for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function() {
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.maxHeight){
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      }
    }
  }
  </script>

I am facing an issue with changing the content of the "Your grade is:" message using innerHTML. I have tried moving the p tag around and changing the values, but nothing seems to work. Any advice on how to resolve this problem would be greatly appreciated as I aim to display quiz results and highlight incorrect answers in the future.
Thank you!

Here is the revised script:

<script>
document.getElementById("submit").onclick = function() {
var result=0;
var count=0;
{% for question in lesson.question_set.all %}
result += parseInt(document.querySelector('input[name = "question{{question.pk}}"]:checked').value);
count++;
{% endfor %}
document.getElementById("grade").innerHTML = "You got a "+result+"/"+count+". If you would like to retake the quiz please resubmit!";
return false; // required to not refresh the page; just leave this here
} //this ends the submit function
</script>

Answer №1

Remember, it is actually getElementById, and not getElementsById.

document.getElementById("grade").innerHTML = result;

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

Adjusting the size of a Google map based on the size of the browser

Currently, I am working on implementing Google Maps API v3. The map is displaying perfectly on my page, but the issue arises when I resize the browser. Upon resizing, the map reverts to its original size as it was when the page initially loaded. This is t ...

positioning two out of three items in a navigation menu to the right and the remaining one to the left

I am looking to design a navigation bar that spans the full width of the screen, with the logo on the left side serving as a link back to the home page. Additionally, I want to include elements for register/login and cart. The background color should cove ...

What is the most efficient way to retrieve the operating system's name and version using JavaScript?

I'm in the process of developing an object that will simplify accessing browser and system information by implementing a function. One particular function within this object is responsible for retrieving the operating system name and version, returnin ...

Connect parent-child models with checkboxes

Currently, I am working on an application where I need to link checkboxes for a role-based menu. The challenge lies in dealing with parent-child checkboxes and ensuring that the values of checkboxes are checked based on user input 1, 1.1, 1.2, 2, 3. Despi ...

Disabling iframe javascript timers when iframe is no longer in the user's view

Currently, I am working on a blog post that will showcase various HTML5/Javascript animations through multiple <iframe> sections. These animations utilize methods such as requestAnimationFrame() and/or setInterval(). Due to limitations within the blo ...

Unexpected Placement of CSS Grid Items

I am currently facing an issue with aligning items in a nested grid using grid-column/grid-row assignment. After setting the template with grid-template-rows:/grid-template-columns, I expected the $50 and Give Now items to appear on row 3, with one in colu ...

code, scripting - modal pop-up

My script currently has a popup window using window.open, but most browsers block this type of popups. I now want to change it to another popup that includes a php script, similar to what I've seen on other sites. It seems like they are using ajax. Ca ...

What is the best way to create a list using only distinct elements from an array?

If I have a collection of different colors: Red Blue Blue Green I aim to extract only the unique colors and store them in an array. Subsequently, I plan to incorporate each color from that array into an existing color list. The desired outcome would l ...

What is the best way to determine which CSS class is shown when the page is loaded using JQuery?

I am facing a dilemma with displaying my site logo. I have both an image version and a text version, and I want to choose which one to display based on the vertical position on the page and the screen width. <a class="navbar-brand" id="top-logo" href=" ...

Having trouble displaying a background image on a React application

Public>images>img-2.jpg src>components>pages>Services.js> import React from 'react'; import '../../App.css'; export default function Services() { return <h1 className='services ...

Running a JS/JSON function in Snowflake that results in a syntax error

Can anyone help me troubleshoot the issue with the following function? I am receiving this error message: SQL compilation error: syntax error line 1 at position 0 unexpected 'function'. Should I be using JSON Script or Javascript for this? func ...

Tips for waiting for a function to finish executing in node.js (using a for loop)

Currently, the issue lies in the fact that the for loop instantly loops through everything. However, I want it to pause before looping because each time the loop runs, it inserts data into the database (which takes time). Therefore, I would like it to wait ...

Search for text within the nearest <p> tag using HTML and jQuery

My table contains different td elements with the same ID, as shown in this foreach loop: <td class="AMLLeft" style="display:inline-block; !important">ID: <p class="important">${item.id}</p> </td> <td align="right" nowrap="tr ...

The bullets in the HTML unordered list are partially hidden from view

My unordered list is experiencing issues with displaying bullets correctly. In Firefox and Internet Explorer, the bullets are only partially shown, while in Chrome they are not visible at all. Adding margin-left: 5px to the <li> element resolves this ...

Absolute positioning causes an element's height to increase

As I delve into the realm of typographical animations and seek to calculate the baseline of a font at various sizes, I've stumbled upon a peculiar discovery: It appears that the height values of elements tend to increase in an erratic manner when thei ...

Retrieve the computed value of a cell in an Excel spreadsheet using Node.js

Utilizing node.js in tandem with the exceljs module to process my Excel sheets. Writing values into specific cells while others already contain formulas. Seeking a method to trigger those formulas and programmatically store the resultant values in the she ...

Exploring the depths of AngularJS through manual injection

I seem to have misunderstood the tutorial and am struggling to get manual injection working on my project. As I'm preparing to minify and mangle my JS code, I decided to manually inject all my modules and controllers. However, I keep encountering err ...

The built-in browser form validation is failing to function properly within a Vuetify v-form component

I want to utilize the default form validation of browsers for basic validations like required and email, while handling more complex validations in my service layer. However, I am having trouble implementing the required and email properties in my form. ...

Ways to properly release file descriptors in write streams

I'm currently working on a code snippet that showcases what I'm aiming to achieve: const fs = require('fs'); var stream = fs.createWriteStream('/tmp/file'); stream.once('open', function(fd) { for (var i = 0; i ...

I encountered an issue with Expo commands where it was throwing an error: "Module not found: 'minizlib'"

Every time I attempt to execute commands like expo init, expo start, or simply expo, it returns the following error message: Error: Cannot find module 'minizlib' Require stack: - /usr/local/lib/node_modules/expo-cli/node_modules/tar/lib/pack.js ...