1

I'm trying to write a script that will search an entire page and remove the attribute disabled from all objects of type button. What is the best method to accomplish this? Looking for plain JS solutions.

For example I would like to turn this:

<button disabled class="foo">My button</button>

into:

<button class="foo">My button</button>

This script will be executed after the page loads.

1
  • I'm going to use this to remove thumbnail spoilers from sports broadcasts that I watch on delay! Seeing a score or players celebrating in a video thumbnail ruins it. Jun 8, 2022 at 3:06

4 Answers 4

13
document.querySelectorAll('button').forEach(b=>b.removeAttribute('disabled'));

should work.

2

You can do that in vanilla JavaScript without any library. Use getElementsByTagName() to get all elements of a given tag, and then iterate through them and use removeAttribute() to remove a given attribute. Here is a demo:

var b = document.getElementsByTagName("button");

for (var i = 0; i < b.length; i++) {
  b[i].removeAttribute("disabled");
}
<button disabled class="foo">My 1st button</button>
<button disabled class="foo">My 2nd button</button>
<button disabled class="foo">My 3rd button</button>

0

You can use this if you need to use values/attributes for individual buttons

$('button').each(function(){
    $(this).prop('disabled', false);
});

OR

You can simply use this

$('button').prop('disabled', false);
4
  • is there any way to crawl the entire page? I'm trying to build this as a browser extension.
    – colindunn
    Mar 1, 2018 at 2:55
  • pasting the second suggestion into the console gives me this error: TypeError: $ is not a function. (In '$('button')', '$' is undefined)
    – colindunn
    Mar 1, 2018 at 2:57
  • Did you embed jQuery into your page? Mar 1, 2018 at 2:58
  • This answer requires jQuery, which is not mentioned in the question.
    – Ash_Crow
    Mar 27 at 17:29
0

If you are using JQuery you can do $('button[disabled]').removeAttr('disabled')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.