The Wayback Machine - https://web.archive.org/web/20240620143222/https://www.geeksforgeeks.org/how-to-loop-through-input-elements-in-jquery/
Open In App

How to loop through input elements in jQuery ?

Last Updated : 01 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to loop through input elements and display their current values in jQuery. This can be done using two approaches:

Approach 1: In this approach, we will iterate over every input type which is of text type by using input[type=text] as the selector. Next, we will use the each() method to iterate over the inputs to display the values or perform any operation as needed.

Syntax:

$("#id input[type=text]").each(function() {
    //... your code
});

Example:

HTML




<html>
<head>
  
  <!-- Include jQuery -->
  <script src=
  </script>
  <script>
    $(document).ready(function () {
  
      // Bind the click event to the function
      $("#buttonId").click(function () {
          
        // Select all the elements with the
        // type of text
        $("#formId input[type=text]")
            .each(function () {
  
          // Print the value currently in
          // the input box
          console.log(this.value);
        });
      })
    });
  </script>
</head>
<body>
  
  <!-- Define the form and the inputs -->
  <form action="" id="formId">
    <label>enter email</label>
    <input type="text"
           placeholder="email"><br>
    <label>enter name</label>
    <input type="text"
           placeholder="name"><br>
    <label>enter city</label>
    <input type="text"
           placeholder="city"><br><br>
    <button type="button" 
            id="buttonId">
      Loop Through
    </button>
  </form>
</body>
</html>


Output:

Approach 2: In this approach, we will try to iterate over all possible input types. We will select the form using the form id and iterate over every input type using the filter() method in jQuery. The inputs can be filtered by specifying the :input selector in jQuery that selects every type of input on the element it is used. Next, we will use the each() method to iterate over the inputs to display the values or perform any operation as needed.

Syntax:

$('#id *').filter(':input').each(function () {
     //..your code
});

Example:

HTML




<html>
<head>
  
  <!-- Include jQuery -->
  <script src=
  </script>
  <script>
    $(document).ready(function () {
  
      // Bind the click event to the function
      $("#buttonId").click(function () {
  
        // Select all the elements
        // which is of the type of input
        $('#formId *').filter(':input')
                        .each(function () {
  
        // Print the value currently in
        // the input element
          console.log($(this).val());
        });
      })
    })
  </script>
</head>
<body>
  
  <!-- Define the form and the inputs -->
  <form action="" id="formId">
    <label>enter email</label>
    <input type="email"
           placeholder="email"><br>
    <label>enter password</label>
    <input type="password" 
           placeholder="password"><br>
    <label>enter city</label>
    <input type="text" 
           placeholder="city">
    <br><br>
    <button type="button" 
            id="buttonId">
      Loop Through
    </button>
  </form>
</body>
</html>


Output:



Similar Reads

How to loop through HTML elements without using forEach() loop in JavaScript ?
In this article, we will learn how to loop through HTML elements without using the forEach() method. This can be done in the following ways: Table of Content Approach 1: Using the for loopApproach 2: Using the While loopApproach 3: Using the 'for.....of' statementApproach 4: Using the for...in statementApproach 1: Using the for loopThe HTML element
4 min read
How to Loop Through an Array using a foreach Loop in PHP?
Given an array (indexed or associative), the task is to loop through the array using foreach loop. The foreach loop iterates through each array element and performs the operations. PHP foreach LoopThe foreach loop iterates over each key/value pair in an array. This loop is mainly useful for iterating through associative arrays where you need both t
2 min read
How to loop through a list of elements and display it in VueJS ?
Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. The v-for directive is used to loop o
2 min read
How to loop through a plain object with the objects as members in JavaScript ?
Looping through a plain JavaScript object with objects as members means iterating over each property of the object and accessing its values, which may also be objects. This is a common task in JavaScript, especially when dealing with JSON data or APIs. There are several ways to loop through a plain JavaScript object with objects as members. Here ar
3 min read
How to Loop through Object with *ngFor in Angular ?
JSON stands for JavaScript Object Notation. It is a format for structuring data. This format is used by different web applications to communicate with each other. In this article, we will learn Loop through Object with *ngFor in Angular. Table of Content Steps for Installing &amp; Configuring the Angular ApplicationProject StructureUsing PipeUsing
3 min read
How to Loop through array of JSON object with *ngFor in Angular ?
JavaScript Object Notation (JSON) is a text-based, human-readable interchange format used for representing simple data structures and objects in web browser-based code. In order to Loop through an array of JSON objects, the *ngFor directive can be utilized that helps to dynamically generate HTML content based on the data in those objects. For insta
3 min read
How to Loop through JSON in EJS ?
EJS stands for Embedded JavaScript. It is a template engine designed to be used with server-side JavaScript environments like NodeJS and It is used to generate dynamic content in a web page. In this article, we will learn about How to Loop through JSON in EJS with different Approaches. Approaches to Create Loop through JSON in EJS:We will go throug
4 min read
What is the fastest way to loop through an array in JavaScript ?
The fastest way to loop through an array in JavaScript depends upon the usecases and requirements. JavaScript has a large variety of loops available for performing iterations. The following loops along with their syntax are supported by JavaScript. Table of Content for loop while loop .forEach() loopfor...of Loopfor loop A JavaScript for loop compr
3 min read
How to Loop through XML in JavaScript ?
In JavaScript, looping over XML data is important for processing and extracting information from structured XML documents. Below is a list of methods utilized to loop through XML. Table of Content Using for loop with DOMParser and getElementsByTagNameUsing Array.from with DOMParser and childNodesUsing for loop with DOMParser and getElementsByTagNam
2 min read
How to loop through an array containing multiple objects and access their properties in JavaScript ?
Looping through an array of elements is straightforward because you can access them using their indexes. However, it's a bit different with objects since they consist of key-value pairs, and you can't index them like an array. To loop through an array of objects in JavaScript, you need to use different methods compared to traditional arrays. JavaSc
4 min read