• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

j script radio button

How do i determine if a radio button is selected??

I am trying this :

var ConTVal = $get("rbDevice").checked;

but it returns false every time
0
troyvw
Asked:
troyvw
  • 6
  • 5
  • 3
  • +2
1 Solution
 
TonyRebaCommented:
<script type="text/javascript">

    function SelectAll(id) {

        var frm = document.forms[0];

        for (i=0;i<frm.elements.length;i++) {

            if (frm.elements[i].type == "checkbox") {

                frm.elements[i].checked = document.getElementById(id).checked;

            }

        }

    }  

</script>

Hope this help u..
0
 
troyvwAuthor Commented:
There isn't a way to do it with one line?
0
 
TonyRebaCommented:
You would have to use Jquery ....
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
TonyRebaCommented:
0
 
leakim971PluritechnicianCommented:
>There isn't a way to do it with one line?

Use : var ConTVal = $("#rbDevice").is(":checked"); // true or false

Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("*").click(function() {
			if( $("#rbDevice").is(":checked") ) alert("thank you, you selected me!");
		});
	});
</script>
</head>
<body>
<input type="radio" name="r" />Don't check me!<br />
<input type="radio" name="r" />Don't check me!<br />
<input id="rbDevice" type="radio" name="r" /><b>Please check me!</b><br />
<input type="radio" name="r" />Don't check me!<br />
</body>
</html>

Open in new window

0
 
troyvwAuthor Commented:
So what would the one line be?

if( $("#rbDevice").is(":checked") = true?

I don't program in java script so I don't know much about the syntax
0
 
leakim971PluritechnicianCommented:
You said :
>I am trying this :
>var ConTVal = $get("rbDevice").checked;

I propose you a working line doing the same thing with one line :
var ConTVal = $("#rbDevice").is(":checked");
0
 
troyvwAuthor Commented:
OK, i will check it out in the morning.... if you don't mind my asking what was wrong with me code?
0
 
leakim971PluritechnicianCommented:
To use it :


if( $("#rbDevice").is(":checked") )
{
  // do something if true
}
else
{
  // do something if false
}

Open in new window

0
 
troyvwAuthor Commented:
var ConTVall = $("#rbDevice").is(":checked")

is giving me false every time...
0
 
leakim971PluritechnicianCommented:
rbDevice is the id of the button radio
0
 
troyvwAuthor Commented:
<input id="rbDevice" type="radio" name="rbgType" value="0" />Device
0
 
troyvwAuthor Commented:
I don't know if this makes a diffrence but there are two rb next to each other: this is the html'

<td style="text-align:left" class="summaryBlackLabel">
                                                            Type:<br />
                                                            <input id="rbDevice" type="radio" name="rbgType" value="0" />Device
                                                        <br />
                                                        <input id="rbControl" type="radio" name="rbgType" value="1" />Controller
                                                        </td>
0
 
leakim971PluritechnicianCommented:
No problem. Check my test page
0
 
WilliamStamCommented:
$("radio:checked").val()
will return the checked value..


if ( $("radio:checked").val()  == "the1iWant" ){
// its checked
}
0
 
Lukasz ChmielewskiCommented:
WilliamStam is absolutely right, :checked selectod should be used that way
http://api.jquery.com/checked-selector/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 6
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now