Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the values of all inputs with same class as an array

Tags:

I have a group of inputs and I want to get the value of each one in array form or in any way that you will suggest. I am not very good at arrays.

$(elemnt).each(function(index, element) {     $('#spc-name').val($(".spocName").val());     alert($(".spocName").val()); }); 

the above line of code alert right thing for me but for a single input only but I have multiple inputs with class="spocName" so I want to get values of all and so that I could then save each in DB table in seperate rows.

like image 478
kwk.stack Avatar asked Oct 08 '13 12:10

kwk.stack


People also ask

How do you get all elements with the same class?

Method getElementsByClassName() returns a set of DOM elements that have a certain class name. Here is a canonical example of how to use the returned list of nodes: var elements = document. getElementsByClassName("class-1"); for (var i = 0, len = elements.

How to get all input values of same class in jQuery?

Explanation: Open an empty array, Use forEach respective to the selector that you want to receive values in. Use jQuery to put empty objects in the array as much as the length of selector. Whenever you input anything in the input field, it would modify it's original value and thus modifying the array itself.

How to loop through class jQuery?

Use each: ' i ' is the postion in the array, obj is the DOM object that you are iterating (can be accessed through the jQuery wrapper $(this) as well). $('. testimonial'). each(function(i, obj) { //test });

How to loop through elements in jQuery?

jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It's very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.


1 Answers

If all your inputs share the same class say "class1" then you can select all such inputs using this

var inputs = $(".class1"); 

Then you can iterate over the inputs any way you want.

for(var i = 0; i < inputs.length; i++){     alert($(inputs[i]).val()); } 
like image 166
AbhinavRanjan Avatar answered Oct 06 '22 03:10

AbhinavRanjan