Solved

j script radio button

Posted on 2010-09-17
19
206 Views
Last Modified: 2013-11-18
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
Comment
Question by:troyvw
[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
  • 6
  • 5
  • 3
  • +2
19 Comments
 
LVL 9

Expert Comment

by:TonyReba
ID: 33705935
<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
 

Author Comment

by:troyvw
ID: 33705950
There isn't a way to do it with one line?
0
 
LVL 9

Expert Comment

by:TonyReba
ID: 33705982
You would have to use Jquery ....
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 9

Expert Comment

by:TonyReba
ID: 33705983
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33706344
>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
 

Author Comment

by:troyvw
ID: 33706358
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33706410
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
 

Author Comment

by:troyvw
ID: 33706429
OK, i will check it out in the morning.... if you don't mind my asking what was wrong with me code?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33706441
To use it :


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

Open in new window

0
 

Author Comment

by:troyvw
ID: 33707651
var ConTVall = $("#rbDevice").is(":checked")

is giving me false every time...
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33707660
rbDevice is the id of the button radio
0
 

Author Comment

by:troyvw
ID: 33707681
<input id="rbDevice" type="radio" name="rbgType" value="0" />Device
0
 

Author Comment

by:troyvw
ID: 33707722
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
 
LVL 82

Expert Comment

by:leakim971
ID: 33707748
No problem. Check my test page
0
 
LVL 2

Expert Comment

by:WilliamStam
ID: 33711308
$("radio:checked").val()
will return the checked value..


if ( $("radio:checked").val()  == "the1iWant" ){
// its checked
}
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34615430
WilliamStam is absolutely right, :checked selectod should be used that way
http://api.jquery.com/checked-selector/
0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
boostsrap 1 32
Horizontal Full Calendar using php 5 42
close window in firefox 3 37
Change background images after 5 seconds. 12 46
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

732 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