?
Solved

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

Posted on 2011-03-02
9
Medium Priority
?
316 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 2000 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

770 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