Checking every radio button

Checking every radio button

Hey guys I am knew to jQuery and I'm not completely sure if i am using the syntax correctly. I would like every radio button to be selected when I click the on the button. If someone could guide me some where or explain what is wrong with my code much would be appreciated. Thank you.



  1. <!DOCTYPE HTML>

  2. <html>
  3. <head>
  4. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

  5. <style>


  6. </style>
  7. <script>

  8. $("input:radio").each(function() {

  9.   if ($(this).attr('checked') != "checked"){

  10.     $(this).attr("checked","checked"); 
  11. });

  12. </script>
  13. </head>
  14. <body>
  15. <form>
  16. <input type="radio" value="male">Male<br>
  17. <input type="radio" value="female">Female
  18. </form>
  19. <form>
  20. <input type="radio" value="male">Male<br>
  21. <input type="radio" value="female">Female
  22. </form>
  23. <form>
  24. <input type="radio" value="male">Male<br>
  25. <input type="radio" value="female">Female
  26. </form>
  27. <form>
  28. <input type="radio" value="male">Male<br>
  29. <input type="radio" value="female">Female
  30. </form>

  31. </body>
  32. </html>
  33.