Solved

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

Posted on 2011-03-02
9
315 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

705 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