Getting a radio element and it’s checked value

Radio buttons in a form can be grouped together using the same name attribute, such that, if one of them is checked, all the others are automatically un-checked.

Let us look at an example:

<input type="radio" id="work_abroad_y" name="work_abroad" value="y" /><label for="work_abroad_y">Yes</label>
<input type="radio" id="work_abroad_n" name="work_abroad" value="n" /><label for="work_abroad_n">No</label>

In order to retrieve the value of the checked radio button, we can write a simple JavaScript function:

function getRadioCheckedValue(radio_name)
{
var oRadio = document.forms[0].elements[radio_name]; for(var i = 0; i < oRadio.length; i++)
{
if(oRadio[i].checked)
{
return oRadio[i].value;
}
} return '';
}

We obtain a reference to the group of radio buttons using the name of the group, and then iterate over all the radio button objects to test which one of them is checked.

Check box

A check box in a form has only two states (checked or un-checked) and is independent of the state of other check boxes in the form, unlike radio buttons which can be grouped together under a common name. Let us look at the HTML code showing two check boxes:

<input type="checkbox" name="email_alerts" id="chk_email_alerts" value="ON" checked><label for='chk_email_alerts'>Email me matching jobs everyday</label>

<input type="checkbox" name="recruiter_contact" id="chk_recruiter_contact" value="ON"><label for='chk_recruiter_contact'>Enable Recruiters to directly contact me</label>

In order to access these checkboxes, their values, and their states, we can use the following javascript function:

function testCheckbox(oCheckbox)
{
var checkbox_val = oCheckbox.value;
if (oCheckbox.checked == true)
{
alert("Checkbox with name = " + oCheckbox.name + " and value =" +
checkbox_val + " is checked");
}
else
{
alert("Checkbox with name = " + oCheckbox.name + " and value =" +
checkbox_val + " is not checked");
}
}

We can then use the function this way:

oCheckBox1 = oForm.elements["email_alerts"];
oCheckBox2 = oForm.elements["recruiter_contact"]; testCheckbox(oCheckBox1);
testCheckbox(oCheckBox2);

Example

See the demo

The textarea for ‘work permit’ gets disabled if ‘No’ is chosen for ‘Willing to work abroad ?’ and vis-versa We write onClick event handler for each of the options. First have a look at the HTML:

Willing to work abroad ?
<input type="radio" name="work_abroad" id="work_abroad_y" value="y" onClick="enableElement(this.form.work_permit);"><label for='work_abroad_y'>Yes</label>
<input type="radio" name="work_abroad" id="work_abroad_n" value="n" onClick="disableElement(this.form.work_permit);"><label for='work_abroad_n'>No</label> <label for="work_permit">Information about acquiring work permit / visa: </label><textarea name="work_permit" rows="3" cols="35"></textarea>

The JavaScript code:

function disableElement(obj)
{
obj.value = ' - N.A. - ';
obj.disabled = true;
} function enableElement(obj)
{
obj.value = '';
obj.disabled = false;
}

Download the code

Prev: How to get the value of a form element : Drop downs and lists

最新文章

  1. 在 Linux 上使用 Jexus + Mono 建立 Asp.Net 网站.
  2. 如何让用户只能访问特定的数据库(MSSQL)
  3. bzoj2006: [NOI2010]超级钢琴
  4. 阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来
  5. hashCode与equals的区别与联系
  6. 欧拉通路-Play on Words 分类: POJ 图论 2015-08-06 19:13 4人阅读 评论(0) 收藏
  7. Ajax 整理总结(入门)
  8. Mosquitto安装_Ubuntu/Debian上安装消息队列Mosquitto
  9. .net中的特性
  10. 如何交叉编译开源库--&gt;编译c-ares库从失败到成功的过程[ocean]
  11. 【前端】一句命令快速合并压缩 JS、CSS
  12. 转:Redis使用认证密码登录
  13. 配置Https 和 HSTS
  14. 【速读】——ResNeXt
  15. 牛客JS编程大题(一)
  16. mongoose 基础api 图表整理
  17. oracle移动数据/修改数据文件路径
  18. LINQ TO SQL 中的join(转帖)
  19. 批处理文件:将目录下所有的jar文件都加到CLASSPATH
  20. 20145324 《Java程序设计》第10周学习总结

热门文章

  1. ADB与AVD的常见问题
  2. C#如何使用SplitContainer控件实现上下分隔
  3. 【deep learning学习笔记】注释yusugomori的DA代码 --- dA.cpp -- 训练
  4. 样条之贝塞尔(Bezier)
  5. 6.1 如何在spring中自定义xml标签
  6. Pascal&#39;s Triangle II Leetcode java
  7. 【Kafka】Kafka为什么要加入分区的概念
  8. Android性能优化系列之App启动优化
  9. Mockito单测,mock service层的mapper
  10. log4j和web.xml配置webAppRootKey 的问题(一个tomcat下部署多个应用)