Solved

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

Posted on 2011-03-02
9
314 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 …
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…

733 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