Activating various input with js

Asked

Viewed 26 times

1

Guys I’m trying to catch several name with same name in js and activate them but unsuccessfully follows the code;

function botao(){

    document.getElementsByName("titulo").disabled=false;
    document.getElementsByName("descriçao").disabled=false;
    document.getElementsByName("data").disabled=false;
    document.getElementsByName("submit").disabled=false;


    }

1 answer

2

That’s because getElementsByName will return you an array, more specifically an NodeList. In assigning false to the property disabled of that return, you will only be creating a property called disabled in the array, but you should be assigning this value to the items within the array, not to the array itself.

Note that in jQuery you can use a method to assign properties to all the results of your query, maybe this is what is causing confusion, but in pure Javascript you need to go through the array and assign the value to each item manually:

function botao() {

    for (var titulo    of document.getElementsByName("titulo"))    titulo.disabled    = false;
    for (var descricao of document.getElementsByName("descriçao")) descricao.disabled = false;
    for (var data      of document.getElementsByName("data"))      data.disabled      = false;
    for (var submit    of document.getElementsByName("submit"))    submit.disabled    = false;

}
  • An interesting alternative would be to use only one for and capture all elements with querySelectorAll.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.