Get all input elements in javascript

Javascript get all input elements in form and all img tags, divs, a tags, buttons and radio buttons

In the front-end design process of a web page, it is common to get all the images or inputs in the web page. Sometimes, a little change to get all the images, divs, inputs, buttons, a tags, buttons and radio buttons, etc in the specified elements, these operations are similar.

Get a specified element using document.getElementById in javascript, and get a class of elements use document.getElementsByTagName, which can be seen literally by tag name to get a class of elements. If the tag is img, then get all images tags in the page; if the tag name is input, all input elements are got, regardless of whether the type is text input box or button.

Читайте также:  Simple python json server

I. Get all img tags javascript

1. Get all img tags in the web page in javascript

var arrImg = document.images;

The «arrImg» is an array, and each image is stored in the array as an element of the array, and the elements can be fetched or traversed according to the array.

var arrImg = document.getElementsByTagName( «img» );

Get it through the image tag img, and save all the pictures to the array «arrImg».

2. Get all img tags in the specified elements in javascript

The got method is the same as above, the code is as follows:

var ul = document.getElementById( «ulId» );
var arrImg1 = ul.getElementsByTagName( «img» );

II. Get all divs javascript

1. Get all divs in the web page in javascript

The method of getting is the same as that of getting images, both use the getElementsByTagName method, the specific code is as follows:

var arrDiv = document.getElementsByTagName( «div» );

2. Get all divs in the specified elements in javascript

If there are specified elements as follows:

< div id ="divId"> < div >Get all divs in the specified elements in javascript

< div >Javascript get a class of element method

Javascript code:

var div = document.getElementById( «divId» );
var arrDiv = div.getElementsByTagName( «div» );

III. Javascript get all input elements in form

1. Javascript get all input elements in form in the web page, the specific code is as follows:

var arrInput = document.getElementsByTagName( «input» );

2. Get the input (button) of the specified element in the web page

If there are specified elements as follows:

Javascript code:

var dl= document.getElementById( «dlId» );
var arrInput = div.getElementsByTagName( «input» );

In this way, both the text box and the button are got. In actual application, it is necessary to determine whether it is a text box or a button according to the type. The method of judgment is as follows:

for ( var i = 0; i < arrInput.length; i++)if (arrInput[i].type == "button" ) // The current element is button
>
>

3. How to get all radio buttons in javascript

(1) How to get all radio buttons in the web page in javascript

Javascript code:

< script type ="text/javascript">
function GetAllRadioButtons() var arrInput = document.getElementsByTagName( «input» ), arrRadio = new Array();
var j = 0;
for ( var i = 0; i < arrInput.length; i++) if (arrInput[i].type == "radio" ) arrRadio[j] = arrInput[i];
j++;
>
>
return arrRadio;
>
var arr = GetAllRadioButtons(); // Call
< / script >

(2) Get all selected radio buttons javascript

Javascript code:

function GetAllSelectRadioButtons() var arrInput = document.getElementsByTagName( «input» ), arrRadio = [];
var j = 0;
for ( var i = 0; i < arrInput.length; i++) if (arrInput[i].type == "radio" && arrInput[i].checked) arrRadio[j] = arrInput[i];
j++;
>
>
return arrRadio;
>
var arr = GetAllSelectRadioButtons(); // Call

IV. Get all a tags javascript

1. Get all a link tags in the web page in javascript

The get method also uses getElementsByTagName method, the code is as follows:

var arrA = document.getElementsByTagName( «a» );

2. Get all a link tags in the specified elements in javascript

If there are specified elements as follows:

Javascript code:

var ul = document.getElementById( «ulId» );
var arrA = ul.getElementsByTagName( » a » );

Источник

Get all input elements in javascript

Last updated: Jan 12, 2023
Reading time · 3 min

banner

# Table of Contents

# Get all of a Form’s elements using JavaScript

To get all the elements in a form:

  1. Select the form element.
  2. Use the elements property to get a collection of the form’s elements.
  3. Optionally, convert the collection to an array using the Array.from() method.

Here is the HTML for the example.

Copied!
doctype html> html lang="en"> head> title>bobbyhadz.comtitle> meta charset="UTF-8" /> head> body> form id="my-form"> input type="text" id="first_name" name="first_name" /> input type="text" id="last_name" name="last_name" /> textarea id="message" name="message" rows="5" cols="30" >textarea> input type="submit" value="Submit" /> form> script src="index.js"> script> body> html>

And here is the related JavaScript code.

Copied!
const form = document.getElementById('my-form'); // ✅ Get all form elements const formElements = Array.from(form.elements); formElements.forEach(element => console.log(element); >); console.log('--------------------------'); // ✅ Get only the input elements in a form const onlyInputs = document.querySelectorAll('#my-form input'); onlyInputs.forEach(input => console.log(input); >);

get all form elements using javascript

We selected the form element using the document.getElementById() method.

Copied!
const form = document.getElementById('my-form');

The elements property on the form returns a collection containing all of the form controls.

Copied!
const form = document.getElementById('my-form'); // ✅ Get all form elements const formElements = Array.from(form.elements);

Источник

Get all input elements in javascript

Last updated: Jan 12, 2023
Reading time · 2 min

banner

# Get all Elements by Type using JavaScript

Use the querySelectorAll() method to get all elements by type.

The method returns a NodeList containing the elements that match the provided selector.

Here is the HTML for the examples.

Copied!
DOCTYPE html> html lang="en"> head> meta charset="UTF-8" /> title>bobbyhadz.comtitle> head> body> input type="text" id="first_name" /> input type="number" id="age" /> script src="index.js"> script> body> html>

And here is the related JavaScript code.

Copied!
// ✅ Get all input elements with type = "text" const elements1 = document.querySelectorAll('input[type="text"]'); console.log(elements1); // 👉️ [input#first_name] // ------------------------------------------------------- // ✅ Get all input elements const elements2 = document.querySelectorAll('input'); console.log(elements2); // 👉️ [input#first_name, input#age] // ------------------------------------------------------- // ✅ Get all input elements const elements3 = document.getElementsByTagName('input'); console.log(elements3); // 👉️ [input#first_name, input#age]

# Get all elements by type attribute

In the first example, we used the document.querySelectorAll method to get all input elements that have a type attribute with a value of text .

Copied!
const elements1 = document.querySelectorAll('input[type="text"]'); console.log(elements1); // 👉️ [input#first_name] console.log(elements[0]); // 👉️ input#first_name

If you want to get a specific element, you can access the NodeList at an index.

JavaScript indexes are zero-based, so the first element in the NodeList has an index of 0 .

# Select all elements that have a type attribute set to text

You can adjust the selector as needed, e.g. the following example selects any type of element with a type attribute set to a value of text (not just input elements).

Copied!
const elements = document.querySelectorAll('[type="text"]'); console.log(elements); // 👉️ NodeList [input#first_name]

# Get all input elements on the page

In the second example, we used the querySelectorAll method to get a NodeList of all of the input tags.

Copied!
const elements = document.querySelectorAll('input'); console.log(elements); // 👉️ NodeList(2) [input#first_name, input#age]

# Iterating over a collection of elements

If you need to iterate over the collection, use the forEach() method.

Copied!
const elements = document.querySelectorAll('input'); console.log(elements); // 👉️ [input#first_name, input#age] elements.forEach(element => console.log(element); >);

# Get all elements by Type using getElementsByTagName

The third example uses the document.getElementsByTagName method to select all input tags.

Copied!
const elements = document.getElementsByTagName('input'); console.log(elements); // 👉️ [input#first_name, input#age]

This serves as an alternative to the querySelectorAll method and you might see the getElementsByTagName method used in older code bases.

# Iterating over an HTMLCollection

The method returns an HTMLCollection , which you have to convert to an array if you need to use methods like forEach() .

Copied!
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // 👉️ [input#first_name, input#age] elements.forEach(element => console.log(element); >);

By converting the HTMLCollection to an array, we are able to use any of the array-supported methods.

You can also use a for. of loop to iterate over the collection.

Copied!
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // 👉️ [input#first_name, input#age] for (const element of elements) console.log(element); >

If you need to get a specific element, access the array at an index.

Copied!
const elements = Array.from(document.getElementsByTagName('input')); console.log(elements); // 👉️ [input#first_name, input#age] console.log(elements[0]); // inptu#first-name console.log(elements[elements.length - 1]); // input#age

The first element in the array has an index of 0 and the last element has an index of array.length — 1 .

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

HTMLFormElement: elements property

The HTMLFormElement property elements returns an HTMLFormControlsCollection listing all the form controls contained in the element.

Independently, you can obtain just the number of form controls using the length property.

You can access a particular form control in the returned collection by using either an index or the element’s name or id attributes.

Prior to HTML 5, the returned object was an HTMLCollection , on which HTMLFormControlsCollection is based.

Note: Similarly, you can get a list of all of the forms contained within a given document using the document’s forms property.

Value

An HTMLFormControlsCollection containing all non-image controls in the form. This is a live collection; if form controls are added to or removed from the form, this collection will update to reflect the change.

The form controls in the returned collection are in the same order in which they appear in the form by following a preorder, depth-first traversal of the tree. This is called tree order.

Only the following elements are returned:

Examples

Quick syntax example

In this example, we see how to obtain the list of form controls as well as how to access its members by index and by name or ID.

form id="my-form"> label> Username: input type="text" name="username" /> label> label> Full name: input type="text" name="full-name" /> label> label> Password: input type="password" name="password" /> label> form> 
const inputs = document.getElementById("my-form").elements; const inputByIndex = inputs[0]; const inputByName = inputs["username"]; 

Accessing form controls

This example gets the form’s element list, then iterates over the list, looking for elements of type «text» so that some form of processing can be performed on them.

const inputs = document.getElementById("my-form").elements; // Iterate over the form controls for (let i = 0; i  inputs.length; i++)  if (inputs[i].nodeName === "INPUT" && inputs[i].type === "text")  // Update text input inputs[i].value.toLocaleUpperCase(); > > 

Disabling form controls

const inputs = document.getElementById("my-form").elements; // Iterate over the form controls for (let i = 0; i  inputs.length; i++)  // Disable all form controls inputs[i].setAttribute("disabled", ""); > 

Specifications

Browser compatibility

BCD tables only load in the browser

Found a content problem with this page?

This page was last modified on Apr 7, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

Оцените статью