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 Like