Is there a way to get all the button tag and their types on a particular page using javascript?
There are three types of buttons: submit — Submits the current form data. (This is default.) reset — Resets data in the current form.
The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.).
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
// ...
}
I tried with the original answer with no 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">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With