Jump to content

Testing a radio button ON or OFF?


MGLP

Recommended Posts

My HTML is:

[While($cntr < $NumRcds)
{$cntr=$cntr+1;
print("[<input type=\"radio\" name=\"choice\" id=\"rcd\" value=\"$Row[6]\"><br>");}]

which gives me many records listed with each having a radio button.

Upon Submit, I want to test if at least 1 button has been checked.

My Java is the following and I've tried various other versions non of which work.

Can you tell me what the code should be.

[function formcheck()
{if(name.choice.checked==true){return true;}
else{alert(\"At least one record must be selected in order to DELETE.\");return false;}}]

Thank you

 

Link to comment
Share on other sites

First, your code is generating duplicate ID attributes, all the checkboxes have the same ID "rcd".

You seem to be just making guesses as to how Javascript works instead of trying to understand it, you should take the time to read through the whole W3Schools tutorial for Javascript.

What you need in your Javascript is to get a list of checkboxes. There are several functions in the Document Object Model that do that, getElementsByName works best here.

function formcheck() {
  var checkboxes = document.getElementsByName("choice");
  var atLeastOneIsChecked = false;
  for(var i = 0; i < checkboxes.length; i++) {
    if(checkboxes[i].checked) {
      atLeastOneIsChecked = true;
      break;
    }
  }
  
  if(!atLeastOneIsChecked) {
    alert("At least one record must be selected in order to DELETE.");
    return false;
  }
}

This code will only work, of course, if your form calls this function during the submit event.

Link to comment
Share on other sites

You need to clarify what you are trying to do. You are not allowed to have two identical id values on the same page because that is illegal HTML. Also these id values seem to be unnecessary -- they don't serve any purpose. Also if these radio buttons all have the same name then ONLY one of them can be checked.

Link to comment
Share on other sites

  • 1 month later...
On 4/5/2018 at 8:24 AM, MGLP said:

My HTML is:

[While($cntr < $NumRcds)
{$cntr=$cntr+1;
print("[<input type=\"radio\" name=\"choice\" id=\"rcd\" value=\"$Row[6]\"><br>");}]

which gives me many records listed with each having a radio button.

Upon Submit, I want to test if at least 1 button has been checked.

My Java is the following and I've tried various other versions non of which work.

Can you tell me what the code should be.

[function formcheck()
{if(name.choice.checked==true){return true;}
else{alert(\"At least one record must be selected in order to DELETE.\");return false;}}]

Thank you

 

What language are you trying to write with?  Java or Javascript?

There is not a native "print" function in Javascript.  If the code is Java, then you would get better answers in that forum.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...