Search code examples
javascripttags

Get all the button tag types


Is there a way to get all the button tag and their types on a particular page using javascript?


Solution

  • Have this code either in the load event of the document, or in the bottom of the HTML:

    var buttons = document.getElementsByTagName('button');
    for (let i = 0; i < buttons.length; i++) {
        let button = buttons[i];
        let type = button.getAttribute('type') || 'submit'; // Submit is the default
        // ...
    }