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.
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.
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.
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 });
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.
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()); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With