Solved

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

Posted on 2011-03-02
9
312 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 8

Expert Comment

by:McNetic
ID: 35022241
Sorry, I was late. Now, in your code theres a missing ) before val().
0
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 63
When we use cache:false ajax? 1 33
Pass asp session variable to a js function in an asp script 6 20
replace link with jQuery 4 15
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

803 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