Hi guys for my first piece of work on javascript i have to check a few html form questions which is provided. But im pretty clueless on how to do it, or even start it. First of all how would i check like a drop down menu had the right chosen value? or how would i count the correct answers ? Sorry just want some idea on how to get started with javascript.

Dani AI

Generated

A practical, minimal plan that fits the thread: give each question a stable name (Q1, Q2, ...), set each option’s value to a canonical key (for example 'a', 'b' or a short id), keep the correct answers in a single object keyed by question name, then iterate the form fields, compare value to the key, and count matches. Using value is important — compare keys, not the visible label text.

const answers = { Q1: 'b', Q2: 'c' };

function gradeForm(form) {
  let correct = 0;
  for (const [name, expected] of Object.entries(answers)) {
    const field = form.querySelector(`[name="${name}"]`);
    if (!field) continue;
    let value = '';
    if (field.type === 'radio') {
      const checked = form.querySelector(`[name="${name}"]:checked`);
      value = checked ? checked.value : '';
    } else if (field.type === 'checkbox') {
      value = field.checked ? field.value : '';
    } else {
      value = field.value;
    }
    if (value === expected) correct++;
  }
  return { correct, total: Object.keys(answers).length };
}

A few clarifications tied to earlier posts: was correct that a form reference can read fields (e.g., document.forms[0]), but a passed form or querySelector is clearer and less brittle. ’s idea of counting is fine, though JavaScript arrays already expose .length. Important notes: for text inputs normalize with trim().toLowerCase() before comparing; ensure every <option> has an explicit value; wrap script in DOMContentLoaded or place it after the form; use preventDefault() on submission if grading client-side.

Security caveat: client-side grading is fine for practice, but server-side verification (PHP) is essential for any real scoring — do not trust values coming from the browser.

Recommended Answers

All 5 Replies

What I'm basically asking is how would i mark it? would i store the answers in an array or something?

It would help to know what you are planning to do with the data you collect from the user. Can you explain?

m not understanding ur problem...wht exactly you are trying to do....like how you want to use drop down.can you send me the
small example of it so that i can better understand it...
but one thing i know is to count the correct answers by using function...
here it is.

Array.prototype.count = function() {
    return this.length;
};

hope it works...

Hi sorry i was totally confused initially on this. Done a bit of research into it now. Basically we have an html form as sort of a test. Then we have to check it with Javascript and then on the server side use php (i'm just doing the javascript for now).

someone said they used this 'forms[0].username' which confused me because i dont know what that would be used for.

Im confused about a lot of it, but even the basics escape me. For instance ;

<p id="Q1"> What's the Swedish Capital?
<br />
<select name="Q1">
<option value="">Choose one from the following:</option>
<option value="a">Paris</option>
<option value="b">Stockholm</option>
<option value="c">Copenhagen</option>
<option value="d">Oslo</option>
</select>
How in javascript can i check the result given? is it something to do with getElementByID? if someone could provide me with the javascipt for this it would be a big help, i basically just want to undestand the syntax to get me started. Is it like (as a function) if (stockholm == Q1)
return true;

So you would need to do something like this:

function validate_form () {
    valid = true;
    if ( document.FORMNAME.Q1.value != "stockholm" ) {
        alert ( "Please fill in the 'Your Name' box." );
        valid = false;
    }
    return valid;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.