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

The Node.js server delivers a different object to the client

I'm facing an issue while trying to send an object from the client to a Node.js server. Here is my Ajax call: $.ajax({ url: config.api.url, type: config.api.method, contentType: config.api.contentType, // application/x-www-form-urlencoded; cha ...

Unique Revision: "Identification Zone within a Single-page Application"

Seeking insights from a seasoned DOM/JS Architect. In reference to another discussion about maintaining a clean id space in a Single Page Application (SPA). Due to certain restrictions, I am unable to utilize data binding frameworks and have to work with ...

Whenever I try to update my list of products, I encounter an error message stating that the property 'title' cannot be read because it is null

I am encountering an issue with editing data stored in the database. When attempting to edit the data, it is not displaying correctly and I am receiving a "cannot read property" error. HTML admin-products.component.html <p> <a routerLink="/ad ...

Is it possible to modify the flex direction in Bootstrap based on specific breakpoints?

I am currently utilizing Bootstrap 4 for my project. I have a div that is styled with "d-flex" and it is meant to display as a row at the "lg" breakpoint (screens 1200px and above). However, on smaller devices such as mobile phones at the "sm" or "xs" brea ...

How to position the navigation bar to the right using bootstrap

I have been experimenting with moving the navigation bar to the right side of the screen in bootstrap4 by using float: right; on the nav a element, but it doesn't seem to be having any effect. I am developing my project using a codepen app. Here is t ...

Is there a way for me to determine the size of this JSON structure?

Can anyone help me figure out the length of this JSON object? I need to know how many data are in it. var ddData = [{ "01":"United States", "02":"United Kingdom", "03":"Aruba", "04":"United Kingdom", ...

A step-by-step guide on using Javascript to transform images into text

Hey there! I'm looking for some help to convert an image into text. The idea is that when someone uploads an image and hits the "Submit" button, the text from the image should display in a textarea below. However, the code I've been working on do ...

Choose children input textboxes based on the parent class during the onFocus and onBlur events

How can I dynamically add and remove the "invalid-class" in my iti class div based on focus events in an input textbox, utilizing jQuery? <div class="form-group col-md-6"> <div class="d-flex position-relative"> & ...

Unable to fetch Twitter data using AJAX

Currently, I'm working on a web page that aims to display tweets from a specific city based on a user's search topic. The Twitter request is being sent successfully, with a response code of 200, and the response data is visible in the Chrome deve ...

Having trouble displaying nested routes in Angular within the view

I'm encountering some issues with child/nested routes in Angular 4. In the app.module.ts file, my imports statement looks like this: RouterModule.forRoot([ { path: 'templates', component: TemplateLandingC ...

"Utilize Angular's functionality to include labels for checkboxes generated using *ngFor directive

I am currently working with Angular 5 My goal is to generate multiple checkboxes using the code below and provide them with labels <input type="checkbox" *ngFor = "let v of options[0].options" [value]="1" [name] = "1"> Typically, when working with ...

Using command line arguments to pass parameters to package.json

"scripts": { "start": "gulp", ... }, I have a specific npm package that I'm using which requires passing parameters to the start command. Can anyone help me with how to pass these parameters in the command line? For example, is it possible ...

Transfer the information of a selected element to a different element

Hello, I am trying to transfer content from a child element to another element. In my HTML setup, there is a hidden div named "DetailsExpanded" and multiple items called "IconWrapper". When an "IconWrapper" is clicked, I want to copy the content of its "I ...

Sliding out the existing content and smoothly sliding in a fresh one upon clicking

Thank you for taking the time to read this. I've created a navigation bar and a few DIVs. What I want to achieve is when a button is clicked, the current DIV slides to the left and out of the page, while a new DIV slides in from the right. [http://w ...

AngularJS: Establishing effective communication channels among directives

I am currently developing a custom directive for an audio player that supports mp3 files. The challenge I'm facing is how to handle multiple instances of the player on a single page. My goal is to ensure that when one player is active, starting anothe ...

An issue arises when trying to import the modal popup

Hey there! I'm currently trying to implement a modal popup in react-bootstrap, but I keep running into an error that says "Props not defined". I followed the instructions from the react-bootstrap documentation, but I can't seem to figure out what ...

Tips for creating a substitute for a standard prototype in JavaScript

Having trouble with a JavaScript interpreter that has a buggy Array.prototype.sort. I want to replace it, at least for testing purposes. I have a function called mySort(array, comparator), but how can I make it work like array.sort(comparator)? I also ne ...

Error found in event.PreventDefault()

I'm currently implementing Twitter Bootstrap on my application. I added e.preventDefault for the link button within $(document).ready(), but it doesn't seem to be functioning properly. Below is the code snippet: Master page: <a id="lnkLogou ...

How can you style an HTML tag using esc_html in CSS?

I've encountered an issue while editing the woocommerce orders.php template. This template is responsible for displaying the user's placed orders. I have identified several variables that require secure coding, such as $date_created or $view_orde ...

Adding retrieved ejs elements

Aim: I am struggling with a button that triggers a function to fetch more items from my Mongoose DataBase and display them in a table row dynamically. I am using the get method to retrieve data from the server side. Despite referring to advice from this po ...