Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 422
user782400 Avatar asked Jul 03 '11 16:07

user782400


People also ask

How many button types are there in HTML?

There are three types of buttons: submit — Submits the current form data. (This is default.) reset — Resets data in the current form.

Which tag is used for button?

The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.).


2 Answers

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
    // ...
}
like image 83
Quentin Avatar answered Sep 20 '22 12:09

Quentin


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">
like image 37
Jorgesys Avatar answered Sep 18 '22 12:09

Jorgesys