0

i want to uncheck a radio button and i used this $("#second").attr('checked', false); if i put it in a function and then i call it with a button it works but if i called it in this function

function myFunction() {
  var choice= document.getElementsByName("radiob");
  var len=choice.length//number of radio buttons
  var wrong;
  var arrayq=["test1","test2"];
  var arraya=["صاروخ","12"];
  var j=0;
  for(i=0;i<len;i++){
    if(choice[i].checked){
      if(choice[i].value==arraya[j]){
        document.getElementById("question").innerHTML = arrayq[j];
        j++;    
        //HERE
        $("#second").attr('checked', false);
        document.getElementById("f").innerHTML ="6";
        document.getElementById("s").innerHTML ="7";
        document.getElementById("t").innerHTML ="12";
      } else {
        wrong++;//number of wrong answers
        document.getElementById("question").innerHTML = arrayq[j];
        j++;
        document.getElementById("f").innerHTML ="6";
        document.getElementById("s").innerHTML ="7";
        document.getElementById("t").innerHTML ="12";
      }
    }
  }
}

i don't know why but whenever i put this code $("#second").attr('checked', false); or an alert message under this if statement if(choice[i].value==arraya[j]) it does not work and i also used $("#second").prop('checked', false;);. please keep in mind that i'm a beginner and these options are just to experiment.

Albzi
  • 15,431
  • 6
  • 46
  • 63
  • 1
    `$("#second")` will only select one radio with `id` second.. can you add the html and explain what you're trying to do..? – T J Jun 10 '14 at 11:48
  • possible duplicate of [How to uncheck a radio button?](http://stackoverflow.com/questions/2117538/how-to-uncheck-a-radio-button) – JJJ Jun 10 '14 at 11:48
  • @TJ No i know i'm just trying to check if it works or not. – user3721585 Jun 10 '14 at 11:55
  • @Juhana I already saw it and i have tried it and it didn't work. – user3721585 Jun 10 '14 at 11:57
  • @user3721585 "if it works or not" if what works..? unless you explain what you're trying to do how do we know what's wrong..? how do we know whether you even have such an element in your html unless you share it..? – T J Jun 10 '14 at 12:03
  • @T J i tried `$(this).prop('checked', false);` and `$("input[name=checkbox]:checked").each(function () { //if your checkbox is checked then you can do what ever you want to do with it inside here,or uncheck it });` and they both didn't work – user3721585 Jun 11 '14 at 06:15

3 Answers3

0
$("input[name=checkbox]:checked").each(function () {

//if your checkbox is checked then you can do what ever you want to do with it inside here,or uncheck it

  });
john
  • 57
  • 1
  • 1
  • 10
0

You miss a semicolon here

var len=choice.length//number of radio buttons

And it is

$("#second").prop('checked', false);

not

$("#second").prop('checked', false;);

You may use chrome's console to get debug information.

Jack Song
  • 478
  • 4
  • 9
0

i figured out why it doesn't uncheck the radio button in the if statement, and the reason why is that it never goes in the if statement the whole time it goes to the else statement. sorry it's a stupid mistake, i apologize for wasting your time.