Skip to content Skip to sidebar Skip to footer

Find And Remove Attribute On All Elements Of A Certain Type

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

Solution 1:

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

should work.


Solution 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>

Solution 3:

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);

Solution 4:

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


Post a Comment for "Find And Remove Attribute On All Elements Of A Certain Type"