Obtain the quiz test score in plain text format without any pop-up messages

I'm looking to add an online quiz feature to my blog. Currently, the code I have displays the result score in a popup when the user clicks on "View Results."

However, I would like to show the result as text within the form itself, similar to the image below.

https://i.sstatic.net/EFJCg.png

var answers = ["A","C","B"], 
    tot = answers.length;

function getCheckedValue( radioName ){
    var radios = document.getElementsByName( radioName ); // Get radio group by-name
    for(var y=0; y<radios.length; y++)
      if(radios[y].checked) return radios[y].value; // return the checked value
}

function getScore(){
  var score = 0;
  for (var i=0; i<tot; i++)
    if(getCheckedValue("question"+i)===answers[i]) score += 1; // increment only
  return score;
}

function returnScore(){
  alert("Your score is "+ getScore() +"/"+ tot);
}
<ul>
  <li>
    <h3>How many letters are there in "ZZ"?</h3>
    <input type="radio" name="question0" value="A">2<br>
    <input type="radio" name="question0" value="B">1<br>
    <input type="radio" name="question0" value="C">3<br>
    <input type="radio" name="question0" value="D">4<br>
  </li>
  <li>
    <h3>How many letters are there in "ZZX"?</h3>
    <input type="radio" name="question1" value="A">2<br>
    <input type="radio" name="question1" value="B">1<br>
    <input type="radio" name="question1" value="C">3<br>
    <input type="radio" name="question1" value="D">4<br>
  </li>
  <li>
    <h3>How many letters are there in "V"?</h3>
    <input type="radio" name="question2" value="A">2<br>
    <input type="radio" name="question2" value="B">1<br>
    <input type="radio" name="question2" value="C">3<br>
    <input type="radio" name="question2" value="D">4<br>
  </li>
</ul>

<button onclick="returnScore()">View Results</button>

Answer №1

In order to display the score, you must create a designated spot in the DOM for it.

<div id="show-score"></div>

Next, utilize the returnScore function to input the score into this designated area instead of using an alert message.

function returnScore(){
    document.getElementById('show-score').innerHTML = 
       "Your score is "+ getScore() +"/"+ tot;
}

To view a working example, visit: https://jsfiddle.net/7Ly00p2z/

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

Antd 4: Updating the value in Form.List dynamically with setFieldsValue

<Form> <Form.List name="projects"> {fields => ... fields.map(field => <Form.Item name={[field.key, "type"]} hidden={true} initialValue={type} />) ... } </Form.List> </Form> ...

Enabling or disabling mouse scroll wheel function across various web browsers

I am facing an issue with enabling/disabling mouse scroll using two buttons: "disable_scroll" and "enable_scroll". The code for disabling scroll works perfectly fine: var cancelscroll = function(e) { e.preventDefault(); }; $("#disable_scroll"). ...

Incorporating CASL with the latest version of Angular, version

I'm currently working on implementing CASL into my Angular application, but I'm having trouble understanding how to integrate it. // Login Component ngOnInit() { var jsonBody = {}; jsonBody['email'] = 'peter@klaven'; ...

Trying to access the label attribute of a select option in Vue.js?

Looking to retrieve the name attribute of a selected option in Vuejs. After experimenting with the value attribute, I found that it worked successfully. Below is the code I used: Tested code: <select id="countryselect" name="country" @change="onChange ...

Searching Text Files Using PHP Based on User Input

I am in search of a way to check through a list of Zip Codes that we serve. We possess a text file filled with zip codes for the areas we cover. I would like to include a form on our website where users can input their zip code to determine if we provide s ...

What is the best way to incorporate a minimum width and maximum width in CSS that add up to 100% when necessary?

Can anyone help me find CSS rules that can set a fixed width for an element (width: 500px) but also allow it to be responsive with max-width: 100% if the container is narrower than the element? I saw this example and it works perfectly: .elem { width: 60 ...

I'm having an issue where I'm trying to post form data using AJAX, but for some reason, the form continues to submit and refresh the page. Can anyone

Recently, I have been working on a small and simple chat application using mainly JQuery and AJAX technologies. Below is the HTML form I have created for this chat application: <form class="chat_form" method="post" id="chat_form" autocomplete="off"> ...

Maintain loading state in parent component while the child component's react query is still loading

Within my React application, I have a parent component that sends a request to our API. While waiting for the response, a loader is displayed on the page. Once the response is received, I iterate through the data and pass each iteration to a child componen ...

Display a series of messages using an Angular directive

Here is a sample HTML code: <section class="correspondence"> <header> <div class="from">{{ message.from }}</div> <div class="when">{{ message.when }}</div> </header> <div class="content"> { ...

How can you implement WriteFile in a GET Request without causing the response to be blocked

During the initialization of the app, I require a specific request to be made. This request entails parsing a portion of the JSON response into JavaScript and then saving it to a file. Simultaneously, the rest of the response needs to be sent to the front ...

Is it possible to utilize promises to execute a function once the initial one has completed its task?

I am struggling to wrap my head around promises. Currently, I am working on a project where I scrape a website using node and NPM, then save the data to a CSV file. While I can successfully retrieve the data with multiple scrapes, I want to ensure that th ...

A step-by-step guide on customizing the background color of a Dialog in Angular Material (Version 16)

I've been attempting to modify the background color of my Angular Material Dialog by utilizing the panelClass property in the MatDialogConfig. Unfortunately, I'm encountering a partial success. I am aiming to set the background color as red (jus ...

Changing a button to text with PHP upon clicking

I am looking to create a button that, upon being clicked, will show a simple "X" on the website in the same location as the button itself. Essentially, the button will vanish and be replaced by an "X". The current code for my button is straightforward: &l ...

I am looking to save the data entered in an HTML form directly into a JSON file within the webpage

I am currently storing the value in an array on the server, but I would like to store it only on the client side within the webpage. I want to write the form data from the HTML form into a JSON file that remains on the page itself and is not sent to the ...

Can we implement CSS that is based on the latest ancestor?

Incorporating CSS, my goal is to implement varying styles to the element labeled with the class test. However, the style should differ based on whether it is situated within an element of class a or class b: <div class="a"> lorem <div class=" ...

What are the benefits of incorporating various custom fonts through @font-face?

I feel like I must be overlooking something very simple. I have been using a single custom font with a normal font face: @font-face { font-family: CustomFont; src: url('CustomFont.ttf'); } Everything works perfectly when I use this font ...

Troubleshooting a NextJS and ExpressJS error related to on-demand entry pinging

A challenge arose as I implemented NextJS with a custom server using Express. The issue surfaced while defining routes in Express. Defining Express routes as shown below resulted in errors: app.get('/:username', handle.profile) app.get('/: ...

What is causing the angular ng-repeat table to not sort correctly?

Currently, I am facing an issue with a table that is being populated by Angular: I have provided the code here so you can see the problem in action: http://plnkr.co/edit/qzY4r2XWq1UUJVrcqBsw?p=preview When I click on the a elements, the sort order change ...

Using Filters in VueJs

I am currently working on creating a small Vue.js 2.0 application where I am utilizing the v-select component from here. The data format that I am dealing with looks something like this: { "model": [ { "id":1, ...

Avoid Having ASP.NET Properties Display as Attributes in Custom Controls

After creating a custom ASP.NET Control that acts as a container with a specified wrapping tag, I encountered an issue: class Section : System.Web.UI.HtmlControls.HtmlGenericControl { public string WrapperTag // Simple interface to base-class TagName ...