Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .each() with input elements

Tags:

jquery

each

input

//save tablet jQuery("#savetablet"+jTablets[i].idtablets).on('click', function() {     alert("alertsepy2...");     console.log(jTablets[i].idtablets);     jQuery("#tablet"+jTablets[i].idtablets+" .detailsrow").each(function( index ) {         $(this).each(function( index2 ) {             console.log($(this).html());         });     }); });  <div class="column0"><input type="text" value="-D"></div> <div class="column1"><input type="text" value="D"></div> <div class="column2"><input type="text" value="D"></div> <div class="column3"><input type="number" value="0"></div> <div class="column4"> <input type="number" value="0"></div> <div class="column5"> <input type="number" value="0"></div> <div class="column6"><input type="number" value="0"></div> <div class="column7"><input type="number" value="0"></div> <div class="column8"><input type="number" value="0"></div> <div class="column9"> <input type="number" value="0"></div> <div class="column10"> <input type="number" value=""></div> <div id="tablet17row0" class="column11">11</div> <div class="column0"><input type="text" value="-D"></div> <div class="column1"><input type="text" value="D"></div> <div class="column2"><input type="text" value="D"></div> <div class="column3"><input type="number" value="0"></div> <div class="column4"> <input type="number" value="0"></div> <div class="column5"> <input type="number" value="0"></div> <div class="column6"><input type="number" value="0"></div> <div class="column7"><input type="number" value="0"></div> <div class="column8"><input type="number" value="0"></div> <div class="column9"> <input type="number" value="0"></div> <div class="column10"> <input type="number" value=""></div> <div id="tablet17row1" class="column11">21</div> 

I have the above jQuery .each() that outputs the attached HTML to the console. In this case I want to extract the val() of only the input elements either of type text or type number. Is there some way to isolate just the input elements so I can get their values out into an array?

Thanks in advance...

like image 833
Eae Avatar asked May 17 '13 04:05

Eae


People also ask

What is the use of jQuery each () function?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How do you iterate through an element in jQuery?

The . each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0.

Can we use for loop in jQuery?

You can use a JavaScript for loop to iterate through arrays, and a JavaScript for in loop to iterate through objects. If you are using jQuery you can use either the $. each() method or a for loop to iterate through an array.


2 Answers

To extract number :

var arrNumber = new Array(); $('input[type=number]').each(function(){     arrNumber.push($(this).val()); }) 

To extract text:

var arrText= new Array(); $('input[type=text]').each(function(){     arrText.push($(this).val()); }) 

Edit : .map implementation

var arrText= $('input[type=text]').map(function(){     return this.value; }).get(); 
like image 69
Karl-André Gagnon Avatar answered Sep 29 '22 12:09

Karl-André Gagnon


Assume if all the input elements are inside a form u can refer the below code.

 // get all the inputs into an array.      var $inputs = $('#myForm :input');      // not sure if you wanted this, but I thought I'd add it.     // get an associative array of just the values.     var values = {};     $inputs.each(function() {         values[this.name] = $(this).val();     }); 
like image 29
SivaRajini Avatar answered Sep 29 '22 11:09

SivaRajini