Skip to content Skip to sidebar Skip to footer

Javascript Radio Button Confirmation

I am trying to add a function, or additional JavaScript to the existing function for a confirmation. I will need a confirmation box once the submit button is clicked saying, 'You h

Solution 1:

Set your radio buttons' values like this:

<td><inputtype="radio"id="examtype"name="examtype"value="GCSE" /> : GCSE<br /><td><inputtype="radio"id="examtype"name="examtype"value="A2" /> : A2<br /><td><inputtype="radio"id="examtype"name="examtype"value="AS"/> : AS<br />

Then use this script in your function to validate form or make it a function and call it from your function validateForm:

var checked = null;
var inputs = document.getElementsByName('examtype');
for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
           checked = inputs[i];
           break;
   }
}
if(checked==null)
{
    alert('Please choose an option');
    returnfalse;
}
else{
    return confirm('You have chosen '+checked.value+' is this correct?');
}

Solution 2:

addthis to your code#function confirmation() {

    for (var i=0; i < document.ExamEntry.examtype.length; i++)
       {
       if (document.ExamEntry.examtype[i].checked)
          {
            var answer = confirm(document.ExamEntry.examtype[i].value)

        if (answer){
            document.ExamEntry.examtype[i].checked = true;

        }
        else{
            document.ExamEntry.examtype[i].checked = false;

        }
          }
    }

    }

Then add to you radio button's

( value="a2" )
( value="a1" )
( value="G1" )

And also add to your radio button's (onclick="return confirmation();") remember to put this in all of you radio buttons soz if this doesn't work.

Post a Comment for "Javascript Radio Button Confirmation"