How to loop through input elements in jQuery ? Last Updated : 01 Apr, 2021 Comments Improve Suggest changes Like Article Like 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= "https://code.jquery.com/jquery-3.6.0.js"> </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= "https://code.jquery.com/jquery-3.6.0.js"> </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: Comment More infoAdvertise with us Next Article How to loop through input elements in jQuery ? R rajatagrawal5 Follow Improve Article Tags : Web Technologies JQuery jQuery-Selectors jQuery-Methods jQuery-Questions +1 More Similar Reads How to Loop Through Array in jQuery? jQuery offers various methods for looping through arrays, enabling operations like data transformation, filtering, and manipulation. Efficient array iteration is crucial for dynamic web applications and interactive content management.Below are the approaches to loop through an array in JQuery:Table 5 min read How to select element by ID in jQuery ? In this article, we are going to see how to select the element by its id using jQuery. To learn about this topic one should have prior knowledge about HTML, CSS, JavaScript and jQuery. Using JavaScript: This question is also solved by a popular JavaScript method called document.getElementById() whic 2 min read How to iterate through child elements of a div using jQuery ? jQuery Selector can be used to find (select) HTML elements from the DOM. Once an element is selected, the jQuery children() method is called to find all the child elements of the selected element. To loop through the child elements, the jQuery each() method is used as demonstrated in the following e 3 min read How to specify the name of an input element? To add the name of an input element, we use the HTML <input> name attribute. The HTML <input> name attribute is used to specify a name for an <input> element. It is used to reference the form data after submitting the form or to reference the element in JavaScript. Syntax:<input 2 min read How to select elements by attribute in jQuery ? jQuery is a lightweight JavaScript library. In the vanilla JavaScript language, the getElementById method is used to select an element. However, jQuery provides a much lighter alternative for the same purpose. The 'jQuery Selector' allows the user to manipulate HTML elements and the data inside it(D 2 min read How to select all on focus in input using JQuery? The select() event is applied to an element using jQuery when the user makes a text selection inside an element or on focus the element. This event is limited to some fields. Syntax: $("selected element").select(); //Select all Input field on focus $("input").select(); //Select Input field on focus 1 min read How to find element with specific ID using jQuery ? The question is to determine whether an element with a specific id exists or not using JQuery. jQuery on() Method: This method adds one or more event handlers for the selected elements and child elements.Syntax: $(selector).on(event, childSelector, data, function, map)Parameters: event: It is requir 3 min read 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 state 4 min read How to select multiple elements using jQuery ? In this article, we will learn how to select multiple elements using JQuery. JQuery is the fastest and most lightweight JavaScript library that is used to simplify the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. JQuery is widely famou 2 min read How to create a Week Input using jQuery Mobile ? jQuery Mobile is a web-based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Week Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr 1 min read Like