0

I am trying to return false if no radio button has been selected using the following script:

function validatefilling(){ var a=document.getElementById("myfilling"); if (fillingselected == 0){ var resultloc=document.getElementById("fillingerror"); resultloc.innerHTML = "<span style='color:red;'>Please select a filling!<span>"; return false; } var resultloc=document.getElementById("fillingerror"); resultloc.innerHTML = ""; return true; } 

It doesn't seem to work however. I am trying to have the message "Please select a filling!" appear if nothing has been selected after the user submits the form. "fillingselected" is the value of the radio form submitted.

0

1 Answer 1

0

Try this

if(!document.getElementById('myfilling').checked) { var resultloc=document.getElementById("fillingerror"); resultloc.innerHTML = "<span style='color:red;'>Please select a filling!<span>"; return false; } else { var resultloc=document.getElementById("fillingerror"); resultloc.innerHTML = ""; return true; } 
Sign up to request clarification or add additional context in comments.

6 Comments

The OP did not indicate that he is using jQuery.
@steve are you happy now?
hmm the error message appears onsubmit but when I select a value it returns the error message again
@Jeb please check my updated answer it won't show you the error message if you select radio button now.
so strange. I think your code is correct but I keep getting the error message onsubmit. Do you think something else could be wrong with my code?
|