Deniss Muntjans
Deniss Muntjans

Reputation: 379

How to increase value of dynamic variable by 1 in the for loop?

Here is my example

for(var z = 1; z <= 26; z++){
    value[z] = $('#DiskSize'+z).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

So how to get these results:

value[1] = $('#DiskSize1').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
value[2] = $('#DiskSize2').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
....
value[26] = $('#DiskSize26').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');

I'm trying to convert z to string (var value = $('#DiskSize'+z.toString()).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');) but still nothing!

Could anyone help me please!

Upvotes: 0

Views: 92

Answers (1)

John S
John S

Reputation: 21482

This is a shot in the dark since you did not show your HTML in the question. It's also not clear what results you are getting.

The .val() function can be used to get the value of <input> elements and other form field elements. But if you want the text inside an element (like a <div>, <span> or <p> element), you should use the .text() function.

Upvotes: 2

Related Questions