Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display a specific div depending on value generated after submit

I am setting up a questioner for some of my students and i am stuck.

I have several questions i need to ask my students. All the questions being yes/no questions being selected from a dropdown. "No" will always remain 0 but "Yes" will have a figure somewhere between 0-100 for each question. I am trying to add those selected values to sum them to a total so i can display a score/result.

I have attached the HTML and JS to sum the values from each of the dropdowns (All fine until here).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function submit() { let total=0; document.querySelectorAll('select').forEach(element => { total += parseInt(element.value); }); console.log(total); }


console.log = function(message) {
document.getElementById('result').innerHTML = message;};
console.log('');
 
 <p> 1. Did you attend summer training?

    <select id="select1">
        <option value="0">NO</option>
        <option value="9">YES</option>
    </select>
</p>

<p> 2. Have you passed all your grades?

    <select id="select2">
        <option value="0">NO</option>
        <option value="22">YES</option>
    </select>
</p>

<p> 3. Have you completed summer assignments?

    <select id="select3">
        <option value="0">NO</option>
        <option value="37">YES</option>
    </select>
</p>

<button onclick="submit()">Submit</button>
    
<div id="result"></div> 
    

My final hurdle is depending on the end result ‘that is when i hit submit’ i want to show a specific div with some text on it right below the result/score for each score range (ie any result between 0-20,20-40,40-60,60-100). So for example if the result falls between 20-30. I want to show up a specific div below the result which will have some explainer text on it and the rest of the divs for each score range be hidden.

I am not well versed in JS and would appreciate some help or guidance. I have added my code for reference

>Solution :

Here you are :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p>
      1. Did you attend summer training?

      <select id="select1">
        <option value="0">NO</option>
        <option value="9">YES</option>
      </select>
    </p>

    <p>
      2. Have you passed all your grades?

      <select id="select2">
        <option value="0">NO</option>
        <option value="22">YES</option>
      </select>
    </p>

    <p>
      3. Have you completed summer assignments?

      <select id="select3">
        <option value="0">NO</option>
        <option value="37">YES</option>
      </select>
    </p>

    <button onclick="submit()">Submit</button>

    <div id="result"></div>
    <div id="comment"></div>
  </body>
</html>

<script>
  function submit() {
    let total = 0;
    document.querySelectorAll("select").forEach((element) => {
      total += parseInt(element.value);
    });
    console.log(total);
    document.getElementById("result").innerHTML = total;

    if (0 < total && total < 20) {
      document.getElementById("comment").innerHTML = "so bad";
    } else if (20 < total && total < 40) {
      document.getElementById("comment").innerHTML = "bad";
    } else if (40 < total && total < 60) {
      document.getElementById("comment").innerHTML = "ok";
    } else if (60 < total && total < 100) {
      document.getElementById("comment").innerHTML = "Great!";
    } else null;
  }
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading