Search code examples
javascripttags

document.getElementsByTagName not working


I would like to use document.getElementsByTagName('input') to set as required (or unset it) for a list of inputs. is it possible?

I've tried:

document.getElementsByTagName('input').required = false;

or (for a different purpose)

document.getElementsByTagName('input').value = ""

but it doesn't seem work.

Moreover: is it possible to catch a certain type of input (i.e. text or radio)?

Thank you!!! ObOnKen


Solution

  • getElementsByTagName() returns a collection of elements so you need to iterate over the collection...

    var elements = document.getElementsByTagName('input');
    
    for(var i = 0; i < elements.length; i++)
    {
        if(elements[i].type == "text")
        {
            elements[i].value = "";
        }
    }