views:

16011

answers:

5

I have radio buttons like this

<td>
    <input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
    <input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
    <input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
    <input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
</td>

This is in a form and when user submits a form I want to make all the radio buttons back to default. Meaning none of them checked.

I have this code but it gives an error saying [0] is null or not an object

$('input[@name="correctAnswer"]')[0].checked = false;
$('input[@name="correctAnswer"]')[1].checked = false;
$('input[@name="correctAnswer"]')[2].checked = false;
$('input[@name="correctAnswer"]')[3].checked = false;

I am doing this for IE 6.

A: 

Why don't you do:

$("#radio1, #radio2, #radio3, #radio4").checked = false;
cloudhead
You can't just set the checked property on the array returned from the jQuery call, you have to use the attr() function to set that property on the items within the array.
bdukes
this is not making then 'unchecked'
+13  A: 
$('input[name="correctAnswer"]').attr('checked', false);

Note: it is important that the second argument be false and not "false" since "false" is not a falsy value. i.e.

if ("false") {
    alert("Truthy value. You will see an alert");
}
lambacck
It is important to pass false rather than "false" for the second argument.
Casebash
The "false" for the second argument is important because the second argument is the value to set. If the second argument is empty, it means give me the value of the first element that matches the selector.
lambacck
+3  A: 

Your problem is that the attribute selector doesn't start with a @.

Try this:

$('input[name="correctAnswer"]').attr('checked', false);
bdukes
A: 

Radio button set checked through jquery:

<div id="somediv" >
 <input type="radio" name="enddate" value="1"  />
 <input type="radio" name="enddate" value="2"  />
 <input type="radio" name="enddate" value="3"  />
</div>

jquery code:

$('div#somediv input:radio:nth(0)').attr("checked","checked");
sonali
A: 

The best way to set radiobuttons state in jquery:

HTML:

<input type="radio" name="color" value="orange" /> Orange 
<input type="radio" name="color" value="pink" /> Pink 
<input type="radio" name="color" value="black" /> Black

Jquery (1.4+) code to pre-select one button :

var presetValue = "black";
$("[name=color]").filter("[value="+presetValue+"]").attr("checked","checked");

To unselect the buttons :

$("[name=color]").removeAttr("checked");
Benjk