Q:

How to get input value dynamically using jQuery?

I'm trying to implement the DRY principle in my programming style.
To prevent misunderstanding about my question above, I will explain it one more time below. I did searching for this solution all the time, but it seems I can't find the right solution.
I want to get each input value in the form dynamically (not a dynamic inputs, add new input dynamically, etc.) without going to type this over and over:
var foo = $('input[name="foo"]').val();
var bar = $('input[name="bar"]').val();

For one or two pieces, it will be all right. But the problem is when I have more than 10 inputs, it's going to be unacceptable.
How can I do this?

A:

You can loop on input which has a name attribute using the each function and get that value.

var arr = [];
$(":input[name]").each(function(index, element) {
  arr.push($(this).val());
});
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='text' name='foo' value='1' />
<input type='text' name='bar' value='2' />
<input type='text' name='foo' value='3' />
<input type='text' name='bar' value='4' />
<input type='text' name='foo' value='5' />
<input type='text' name='bar' value='6' />
<input type='text' value='7' />

