0

I have two Buttons like this:

var btn1 = $("#myButton1");
var btn2 = $("#myButton2");

Then I disable those Buttons:

btn1.prop('disabled', true);
btn2.prop('disabled', true);

I tried to make a fast way by calling function like this:

$(btn1, btn2).prop('disabled', true); //just btn1 have prop assign
//or
[btn1, btn2].prop('disabled', true); //error here

How can I disable the Buttons like this (i.e., without an error or a missing assign)?

gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
FireKai
  • 1
  • 2
  • Possible duplicate of [jQuery, same function for multiple ids](https://stackoverflow.com/questions/11151157/jquery-same-function-for-multiple-ids) – Aakash Verma Jun 30 '17 at 08:25

3 Answers3

2

You can use the following to achieve this:

$("#myButton1, #myButton2").prop("disabled", true)
Andrew Burns
  • 324
  • 4
  • 10
0

You need to do it like this

$("#btn1, #btn2").prop('disabled', true);

Look at the docs.

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
0

According the documentation you must comma-seperate the elements like this;

$("#myButton1, #myButton2").prop('disabled', true);

Or you can wrap the buttons in a span or div and use the following (given #myButton1 and #myButton2 are button elements);

$("#mySpan button").prop("disabled", true);
Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49