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,135
  • 5
  • 23
  • 21

5 Answers5

0

Just use the string concatenation, or + operator:

array.push($("#" + i).val());
Alex
  • 61,555
  • 46
  • 149
  • 178
0

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

Anomie
  • 89,707
  • 13
  • 124
  • 144
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());
}
ctcherry
  • 27,308
  • 6
  • 64
  • 71
0
$('input[id]').each(function() {
    array.push($(this).val());
});

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

moe
  • 27,706
  • 3
  • 18
  • 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
  • 137,034
  • 36
  • 218
  • 257