14

I have an MVC view with a number of buttons on it (each item in a basket renders 2 buttons)...

<button class="pbbg" id="ProductMinus_161637" type="button">-</button>
<button class="pbbg" id="ProductPlus_161637" type="button">+</button>

(they both have an onclick event)

When either of these buttons are pressed I want to disable all the buttons for every product until the basket has finished updating.

The click event calls a JavaScript function which in turn makes an Ajax call. Following this post the first thing I try to do is disable all the buttons.....

$("input[type=button]").attr("disabled", "disabled");

Then after the Ajax call returns reenable them....

$("input[type=button]").removeAttr("disabled");

I am getting no errors but the buttons are not disabled.

Where I am going wrong?

Community
  • 1
  • 1
Fred
  • 5,663
  • 4
  • 45
  • 74
  • Try using prop $("input[type=button]").prop("disabled", true); – Adil May 27 '15 at 10:13
  • 5
    Your elements are not `inputs` (they are buttons!) - change your html to `` (and use `.prop('disabled', true)`) –  May 27 '15 at 10:14
  • @StephenMuecke Doh! Thanks I didn't spot that. Took the html straight from the designer. – Fred May 27 '15 at 10:17

5 Answers5

35

Your selector is wrong. instead of input.. selector you should use :button pseudo-selector.

You can use :button selector to select all the buttons.

$(':button').prop('disabled', true); // Disable all the buttons

To enable all the buttons:

$(':button').prop('disabled', false); // Enable all the button

EDIT

If you want to disable only the buttons whose id starts with Product use:

$('button[id^="Product"]')
Tushar
  • 85,780
  • 21
  • 159
  • 179
5

If anybody is looking for a vanilla javascript solution:

const buttons = document.getElementsByTagName("button");
for (const button of buttons) {
  button.disabled = true;
}
bancer
  • 7,475
  • 7
  • 39
  • 58
3

Probably because you're not using the input tag, you're using the straight button tag. Try $("button").attr("disabled", "disable") instead.

jered
  • 11,220
  • 2
  • 23
  • 34
2

You need to use button selector. Try prop like this

$('button').prop('disabled', true);
Zee
  • 8,420
  • 5
  • 36
  • 58
-1

if you want try this .

const buttons = document.getElementById("button"); buttons.setAttribute('disabled', true);