Getting and using values ​​obtained from several text fields through jQuery

I am trying to get values ​​from multiple text fields using jQuery.

I'm a little new to Javascript in general. I have a form with the following input element:

<input name="milkman" value="jessie"/> <input name="letterman2" value="jim" /> <input name="newmilk" /> 

I get the values ​​of the first two input elements using:

 var test_arr = $("input[name*='man']").val(); 

How to get individual text field values? When I use the alert () function to echo the value of test_arr , all I see is the first value of the element.

Please help.

+7
jquery
source share
3 answers

Your sample returns a value only in the first element of the array. You need to iterate over the array, and you can use each . The jQuery syntax syntax returns a jQuery object that contains the mapped objects as an array.

You can also use another variant of $.each , so ...

 var test_arr = $("input[name*='man']"); $.each(test_arr, function(i, item) { //i=index, item=element in array alert($(item).val()); }); 

Since the jQuery return object is an array of matching elements, you can also use the traditional for loop ...

 //you can also use a traditional for loop for(var i=0;i<test_arr.length;i++) { alert($(test_arr[i]).val()); } 
+7
source share

Check jquery each ()

0
source share

Use each :

 var test_arr = $("input[name*='man']").each(function() { var current = $(this).val(); }); 
0
source share

All Articles