1

I have 2 buttons yes and no, and simply I am toggling them.

<!DOCTYPE html>
   <html>
      <head>
         <title>Demo Project</title>
      </head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <script src=""></script>
      <script>  
          function enableCheckBoxAndDisableNoRadioButton() {
             $(".checkbox").attr("checked",true);
             $("#no").attr("checked", false);
          }

          function disableCheckBoxAnddisableYesRadioButton() {
             $("#yes").attr("checked", false);
             $(".checkbox").attr("checked",false);
          }
      </script>
      <body>
         <input class="checkbox" type="checkbox" >
         <p>yes <input id="yes" type="radio" onchange="enableCheckBoxAndDisableNoRadioButton()"></p>
         <p> no<input id="no" type="radio" checked=true onchange="disableCheckBoxAnddisableYesRadioButton()"></p>
      </body>
   </html>

When I click at yes, that time no button getting deselected and checkbox getting selected. But when I click at the no button, that time checkbox getting deselected but yes button is selected too. I want to make it deselect.

Let me know what is the problem.

I am running this code on chrome: 53.0.

Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
Jitendra Yadav
  • 150
  • 1
  • 3
  • 16

4 Answers4

5

Use prop insted of attr.

function enableCheckBoxAndDisableNoRadioButton() {
    $(".checkbox").prop("checked", true);
    $("#no").prop("checked", false);
  }

  function disableCheckBoxAnddisableYesRadioButton() {
    $("#yes").prop("checked", false);
    $(".checkbox").prop("checked", false);

  }

Working Fiddle

4b0
  • 21,981
  • 30
  • 95
  • 142
1

in place of attr, i changed it to .prop and it is working now.

Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
Jitendra Yadav
  • 150
  • 1
  • 3
  • 16
0

change .attr to .prop then it worked

Taufik Nur Rahmanda
  • 1,862
  • 2
  • 20
  • 36
0

You need to check where we can use .prop() / .attr()

Use .prop() instead of .attr() in the most of cases.

See some list of attributes and property below:

| **Attribute/Property** | **Attribute**  | Property

| accesskey              |      Yes       |    No
| align                  |      Yes       |    No
| async                  |      Yes       |    Yes
| autofocus              |      Yes       |    Yes
| checked                |      Yes       |    Yes
| class                  |      Yes       |    No
| href                   |      Yes       |    No
| multiple               |      Yes       |    Yes
| readonly               |      Yes       |    Yes

and read more about .prop() vs .attr()

Community
  • 1
  • 1
Hidayt Rahman
  • 2,490
  • 26
  • 32