Get all types of button tags

Is there a way to get all button tags and their types on a specific page using javascript?

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

I tried with the original answer without success, so I use this:

 var elements = document.querySelectorAll("input[type=button]"); 

Example:

 var elements = document.querySelectorAll("input[type=button]"); for(var i = 0, len = elements.length; i < len; i++) { console.log("Button: " + elements[i].id); } 
  <input type="button" id="alfa" value="alfa"> <input type="button" id="beta" value="beta"> <input type="button" id="gamma" value="gamma"> <input type="button" id="omega" value="omega"> 
0
source

Source: https://habr.com/ru/post/891912/


All Articles