Solved

function to say whether a radio button is checked not returning anything

Posted on 2011-03-02
9
310 Views
Last Modified: 2012-05-11
Attached function is supposed to tell me whether or not a radio button called "selectprogram" is checked. There could be many of these radio buttons on the screen with the same name. I just need to know if one of them is checked, or if none are checked. I run the function, I get no results at all. Syntax problem?
<script language="javascript" type="text/javascript">
	function refreshpage(displaycat){
		/*document.mainform.displaycat.value=displaycat;*/

		var inputVal = $('input[name='selectprogram']:checked', '#mainform').val()

		if (inputVal !== ""){
			alert ("nothing");
		} else {
			alert ("something");
		}

		/*document.mainform.submit();*/
	}
</script>

Open in new window

0
Comment
Question by:bbdesign
  • 4
  • 3
  • 2
9 Comments
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 35022113
The context must be
A DOM Element, Document, or jQuery to use as context
.
Use double quotes in your selector and single quotes in your attribute selector.

So use this
$("input[name='selectprogram']:checked", $("#mainform"))

Open in new window

0
 

Author Comment

by:bbdesign
ID: 35022172
Here is what I have now. I cannot run the function at all, Firebug says refreshpage is not defined. So something isn't quite right.
<script language="javascript" type="text/javascript">
	function refreshpage(displaycat){
		/*document.mainform.displaycat.value=displaycat;*/

		var inputVal = $("input[name='selectprogram']:checked", $("#mainform").val();

		if (inputVal !== ""){
			alert ("nothing");
		} else {
			alert ("something");
		}

		/*document.mainform.submit();*/
	}
</script>

Open in new window

0
 
LVL 8

Accepted Solution

by:
McNetic earned 500 total points
ID: 35022223
First: Did you check your javascript was loading without errors? You tried to add ' quotes into a string surrounded by ' quotes - replace the outer quotes by ".

Second, your selector is probably not what you wanted:

$("input[name='selectprogram']:checked", '#mainform')

Open in new window

This selects all selected inputs with name selectedprogram + all elements with id mainform. What you probably wanted was the same inputs, but only inside mainform - that would be
$("#mainform input[name='selectprogram']:checked")

Open in new window


Third, if none of the radios is selected, your result will not be "" but rather undefined, so you should check for this.
0
 
LVL 8

Expert Comment

by:McNetic
ID: 35022241
Sorry, I was late. Now, in your code theres a missing ) before val().
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:bbdesign
ID: 35022261
OK, here is what I have now. When I run the function, it says "nothing" no matter what... if a radio button is checked or if none of them are checked. Firebug = no errors currently.

Sorry, I am new to jQuery and trying to learn, but the syntax is driving me crazy.
<script language="javascript" type="text/javascript">
	function refreshpage(displaycat){
		/*document.mainform.displaycat.value=displaycat;*/

		var inputVal = $("#mainform input[name='selectprogram']:checked").val();

		if (inputVal !== ""){
			alert ("nothing");
		} else {
			alert ("something");
		}

		/*document.mainform.submit();*/
	}
</script>

Open in new window

0
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 500 total points
ID: 35022492
As I told you, the result of val() will be undefined if no radio button is selected, and a string containing a value if not. So, you have to test for undefined:
if (undefined == inputVal) {
  alert("nothing");
} else {
  alert("something");
}

Open in new window

0
 

Author Comment

by:bbdesign
ID: 35022590
Ah, works now finally! Thank you!
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 35024975
@McNetic : provided selector is correct. Element with id mainForm is the context.
0
 
LVL 8

Expert Comment

by:McNetic
ID: 35026331
@AlbertVanHalen: My answers was only referring to the original question, where the context was not correct. I did, however, not see the author was trying to set context.

However, I think the solution without conext should be (marginally) more efficient, as $() is called once fewer. Context is most helpful wenn context element is already jqueryfied.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now