Jump to content

Checking A Radio Button Value?


Recommended Posts

Hi, I have a form that has input boxes for users to type their information, and dropdown menus for users to select an option. I am trying to also use a radio button, where the question is a Yes or No answer, and the user clicks on either yes or no. I want to use javascript to be able to ensure that a button has been checked, but I don't know what to use. For example, the "First Name" input box has this JS to check whether or not information has been entered:

if(document.quotesform1.FirstName.value.length ==0){												message = message+"Please enter a First Name.\n";								}

The dropdown menu has this JS to check whether or not an option has been chosen:

if(document.quotesform1.InterestedIn.selectedIndex == 0){										message = message+"Please enter your Interest.\n";								}

Where "0" is the value given to the first option ("Select one")How can I use the same JS to check whether or not a radio button has been selected?Thank you very much, any help is greatly appreciated.

Link to post
Share on other sites
Give the radio button an ID and use document.getElementById to access it, you can use the checked property of the element to determine if it was selected.
I've given the radio buttons an ID, here is the code:
Yes <input type="radio" id="CurrentPlan" name="CurrentPlan" value="Yes"> No <input type="radio" id="CurrentPlan" name="CurrentPlan" value="No">

I'm not familiar with the getElementById, how do I write it out so that it checks if it was selected?

Link to post
Share on other sites
2 elements can't have the same ID, each ID on a page must be unique. document.getElementById just returns an element object. In this case, it would return one of these:http://www.w3schools.com/jsref/dom_obj_radio.asp
Ok I've set my two ID's to be different. I still don't know how to check it in the javascript. Any suggestions?
Link to post
Share on other sites

document.getElementById("CurrentPlan").checkedThis only works with radio buttons and checkboxesYou can learn a lot just by looking at an elements properties. jsg pointed you to the correct page. Clicking one link after another and reading them would have turned up this statement very quickly: "The checked property sets or returns whether or not a radio button is checked." --followed by an illustration

Link to post
Share on other sites
document.getElementById("CurrentPlan").checkedThis only works with radio buttons and checkboxesYou can learn a lot just by looking at an elements properties. jsg pointed you to the correct page. Clicking one link after another and reading them would have turned up this statement very quickly: "The checked property sets or returns whether or not a radio button is checked." --followed by an illustration
So in my javascript, would I have something like this:
if(document.quotesform1.getElementById("CurrentPlan").checked ==false){										message = message+"Please select a Current plan.\n";								}

?That doesn't work when I hit submit on the form.

Link to post
Share on other sites
The getElementById method is an element on the document object, not the quotesform1 object. You should also be checking for error messages, an error console would have told you that.
So where does document.getElementById("CurrentPlan").checked go in my JS? what do I add to it in order to display a message if a radio button is unchecked?
Link to post
Share on other sites
I've got to admit that I'm having a hard time understanding why you aren't able to figure this out.
Well I'm in the third week of my first scripting class and I've had limited experience writing my own script. Please understand that I greatly appreciate your help working through this with me despite my lack of knowledge.In my form I have this code for the radio buttons:
Yes <input type="radio" name="CurrentPlan" id="cpyes" value="Yes"> No <input type="radio" name="CurrentPlan" id="cpno" value="No">

And in my JS I have this:

if(document.getElementById("CurrentPlan").checked == false){									  message = message+"Please select a Current plan.\n";									}

But when I hit submit I get an error that says "document.getElementById("CurrentPlan") is null"It seems correct to me so I don't understand the error.

Link to post
Share on other sites

When I have my form set up like this:

Yes <input type="radio" name="CurrentPlan" id="CurrentPlan" value="Yes"> No <input type="radio" name="CurrentPlan" id="CurrentPlan2" value="No">

and the JS:

if(document.getElementById("CurrentPlan").checked == false){message = message+"Please select a Current plan.\n"; }

I get an error message only if "Yes" isn't selected. If "No" is selected, I still get the error message.

Link to post
Share on other sites
if(document.getElementById("CurrentPlan").checked == false){message = message+"Please select a Current plan.\n"; }

I get an error message only if "Yes" isn't selected. If "No" is selected, I still get the error message.

You need to modify your if statement to check for both CurrentPlan and CurrentPlan2 using the && operator.http://w3schools.com/js/js_comparisons.asp Edited by jkloth
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...