0
for(var i=1;i<=($("input:regex(id,[0-9]+)").length);i++)
{
    array.push($("#i").val());
} 

Hey, I am tying to store the input value in the array. Each of the input has distinct id associated with it, in this case, 1,2,3,4 and so on. How can I change the i in

array.push($("#i").val());

accordingly with the counter i in the for loop, which would fetch the value from inputs. like if I got two inputs fields, whose ids are 1 and 2. After this codes execution, the array has two elements that are from the inputs. Thank you

Clinteney Hui
  • 4,385
  • 5
  • 24
  • 21

5 Answers5

0

Just use the string concatenation, or + operator:

array.push($("#" + i).val());
Alex
  • 64,178
  • 48
  • 151
  • 180
0

How can you change the i in the expression? Do you mean like array.push($("#"+i).val());?

Anomie
  • 92,546
  • 13
  • 126
  • 145
0

This should do, just use a + sign to concat the string of "#" with the number from i

for(var i=1;i<=($("input:regex(id,[0-9]+)").length);i++)
{
    array.push($("#"+i).val());
}
Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
0
$('input[id]').each(function() {
    array.push($(this).val());
});

Hope I got it right because It is hard to understand your question!

moe
  • 28,814
  • 4
  • 19
  • 16
0

You could map the values instead of looping over each one of them.

var array = $("input:regex(id,[0-9]+)").map(function() { 
    return this.value;
}).get();
Anurag
  • 140,337
  • 36
  • 221
  • 257