0

I have two buttons but I need to Disable and enable these two buttons alternately. So when I click buttonx it will be disabled and buttony will be enabled, then when I click buttony it will be disabled and buttonx will be enabled again, but in my code it not working.

<script>
$(document).ready(function() {
    $("#buttonx").click(function() {
        $('button').attr('disabled','disabled');
        });

    $("#buttony").click(function() {
        $('button').removeattr('disabled','disabled');
    });
});

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Noel_Ed
  • 19
  • 6

3 Answers3

0

Try this, assuming the buttons in question are the only ones with class button

$(function() {
  $(".button").click(function() {
    $('button').not(this).attr('disabled',false);
    $(this).attr('disabled',true);
  });
});

There is also a radio alternative Making radio buttons look like buttons instead

mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

A code to start with:

$(document).ready(function() {
    $("#buttonx").click(function() {
        $('#buttony').removeAttr('disabled');
        $(this).attr('disabled', 'disabled');
    });

    $("#buttony").click(function() {
        $('#buttonx').removeAttr('disabled');
        $(this).attr('disabled', 'disabled');
    });
});
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

Try this:

$(document).ready(function() {
  $("#buttonx").click(function() {
        $(this).attr('disabled','disabled');
        $('#buttony').removeAttr('disabled');
  }); 

  $("#buttony").click(function() {
      $(this).attr('disabled','disabled');
      $('#buttonx').removeAttr('disabled');
  });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<button id="buttonx">
button 1
</button>

<button id="buttony">
button 2
</button>
zb22
  • 3,126
  • 3
  • 19
  • 34