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(
To get the values of each element as an array you can use map()
:
var valueArray = $('.spocName').map(function() {
return this.value;
}).get();
Or in ES6 (note that this is unsupported in IE):
var arr = $('.spocName').map((i, e) => e.value).get();
You can then use this array as required to save to your database - eg. as a parameter in an AJAX request.
var values = [];
$('.spocNames').each(function(){
values.push({ name: this.name, value: this.value });
});
//use values after the loop
console.log(values);
you can user jquery each function ...
$('.spocNames').each(function(){
alert(this.value);
}
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());
}
@mandip-darji's answer is totally acceptable and working. I did little enhancement to it. So in my case, if you want a value of only one input field.
You can do something like below.
let keyWord = null;
$('.searchinventory_input').each(function(){
if(this.value){
keyWord = this.value;
return false;
}
});