Solved

radio button value

Posted on 2016-10-29
3
93 Views
Last Modified: 2016-10-31
If an html page contains several radio buttons with the same name (but different values) and NONE is selected (checked), is the value of the radio button element null?

For example, in a form named "st", radio button name "pid", does document.st.pid.value == ""?
0
Comment
Question by:Richard Korts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 41865445
if none are selected then yes it's null (not empty string)

What happens for you when you try it?
0
 
LVL 56

Expert Comment

by:Julian Hansen
ID: 41865471
It's more than null it is not defined.

Consider this code
<form action="reflect.php" method="post">
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window

reflect.php
<?php
echo "<pre>" . print_r($_POST, true) . "</pre>";

Open in new window


If you don't click any radios you get an empty $_POST array.

There is a trick you can do to get around this

<form action="reflect.php" method="post">
   <input type="radio" name="testradio" value="0" checked />
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window


In the above we add a 4th input that acts as a default. This is checked by default and if no other buttons are selected then you get a value of 0 coming through.

You can optionally hide this additional radio with styling so it is not visible. It can be useful though if you want the option to de-select a radio - i.e. being able to "change your mind" after clicking a radio to effectively be "none of the above" = which is not practically possible with a standard bank of radio buttons showing only available answers where "changed my mind" would require being able to click "off" a selected radio the way you would a checkbox.
0
 
LVL 43

Expert Comment

by:Rob
ID: 41865481
yes on the server side it will not come through (checkboxes) are the same.

Another trick is to include a hidden element with the default value:

<form action="reflect.php" method="post">
   <input type="hidden" name="testradio" value="0"  />
   <input type="radio" name="testradio" value="1" />
   <input type="radio" name="testradio" value="2" />
   <input type="radio" name="testradio" value="3" />
   <input type="submit">Submit</input>
</form>

Open in new window


However, you are correct that on the client side it will be returned as the empty string "", not null
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Popup write two lines 3 22
null, truthy, falsey confusion 25 33
javascript and jquery question 4 26
Ajax on ASP 2 42
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question