views:

136

answers:

6

On my form I havea set of radio buttons. Here's the mark up:

<div class="optionHolder">
    <p class="optionName">Format</p>
    <div class="option checked">
        <input type="radio" name="fileType" value="avi" />
        <img src="images/avi.png" alt="" />
        <label>AVI</label>
    </div>
    <div class="option">
        <input type="radio" name="fileType" value="mov" />
        <img src="images/mov.png" alt="" />
        <label>MOV</label>
    </div>
    <div class="option">
        <input type="radio" name="fileType" value="mp4" />
        <img src="images/mp4.png" alt="" />
        <label>MP4</label>
    </div>
    <div class="option">
        <input type="radio" name="fileType" value="mp3" />
        <img src="images/mp3.png" alt="" />
        <label>MP3</label>
        </div>
</div>

When the form is submitted I want to check that one of them is checked. What's the best way to go about this? I was thinking of looping through them all and making a flag to set if one of them is checked, and then check the flag after the loop and if it's false throw an error.

Any help is appreciated, cheers.

A: 

You could check to see if the checked radio button name returns a value in jQuery:

if($("input[@name='fileType']:checked").val() != null){
    // button checked
}
Andy Rose
-1 No, `val()` returns `null` when no elements are found, and certainly `null !== ""`
Josh Stodola
My bad, edited to show correct way of checking.
Andy Rose
+2  A: 

Try the jQuery Validation plugin. It can do a lot for you and be really useful for lots of different forms. If you want to do it very simply:

if($("input[name=fileType]:checked").length > 0) {
   //Is Valid
}
ICodeForCoffee
Beat me by 20 seconds. :)
kitsched
@kitsched Questions like this get answered really fast!
ICodeForCoffee
A: 

I think $('input[name=fileType]:checked').length will do the trick.

kitsched
+2  A: 

demo

http://jsfiddle.net/Vq2jB/2/

var isChecked = jQuery("input[name=fileType]:checked").val();
Praveen Prasad
Your way works, but if nothing is selected, your getting undefined instead of false for isChecked. If I'm going to all the var isChecked, I want it to be either true or false. Not true or undefined. Yes, if I go if isChecked it works the same way, but it feels slightly off to me anyway.
ICodeForCoffee
i have added a demo and fixed that problem.
Praveen Prasad
+4  A: 

You can use the length and equal attribute selector with :checked filter selector like this:

if ($("input[name='fileType']:checked").length > 0){
  // one ore more checkboxes are checked
}
else{
 // no checkboxes are checked
}
Sarfraz
You need to check length to be `> 0`, not just defined
Josh Stodola
@Josh Stodola: You should have tested it before down vote :)
Sarfraz
@Sarfraz I did. And I did not down-vote. You need to compare it with zero. Just checking if(.length) checks if it is defined or not. When set to 0, it is still defined. This code does will not work consistently.
Josh Stodola
@Josh Stodola: Modified as per what you said :)
Sarfraz
A: 

Try:

var checkbox = $("input[@name='fileType']:checked");

if( checkbox.length > 0 ) {
    alert( checkbox.val() ); // checkbox value
} else {
    alert('Please select a format'); // error
}

http://jsfiddle.net/wE4RD/

NAVEED