Benjamin
Benjamin

Reputation: 59

Disabling multiple buttons with the same class attribute but different ids

I have multiple buttons with the same class attribute but different ids. I tried disabling them but failed. submit submit

document.querySelector(".submit").disable = true;

Upvotes: 2

Views: 1090

Answers (2)

ZnayTheTiger
ZnayTheTiger

Reputation: 21

Jquery disable button:

$('.submit').attr('disabled',true);

or

$('.submit').prop("disabled", true);

or the old way :

var buttons= document.getElementsByClassName("submit");
for(var i = 0; i < buttons.length; i++) {
    buttons[i].disabled = true;
}

Upvotes: 2

gaetanoM
gaetanoM

Reputation: 42044

While .querySelector() returns only the first element you need .querySelectorAll() in order to disable all buttons with the same class. Instead of disable you need to use disabled and a loop like forEach:

document.querySelectorAll(".submit").forEach(e => e.disabled = true)
<button type="button" class="submit">1</button>
<button type="button" class="submit">2</button>
<button type="button" class="submit">3</button>
<button type="button" class="submit">4</button>

Upvotes: 4

Related Questions