Jump to content

Recommended Posts

How can i shorten this code?

 

var quantity1 = +document.AddPO.quantity1.value;
var quantity2 = +document.AddPO.quantity2.value;
var quantity3 = +document.AddPO.quantity3.value;
var quantity4 = +document.AddPO.quantity4.value;
var quantity5 = +document.AddPO.quantity5.value;
var quantity6 = +document.AddPO.quantity6.value;
var quantity7 = +document.AddPO.quantity7.value;
var quantity8 = +document.AddPO.quantity8.value;
var quantity9 = +document.AddPO.quantity9.value;
var quantity10 = +document.AddPO.quantity10.value;
var quantity11 = +document.AddPO.quantity11.value;
var quantity12 = +document.AddPO.quantity12.value;
var quantity13 = +document.AddPO.quantity13.value;
var quantity14 = +document.AddPO.quantity14.value;
var quantity15 = +document.AddPO.quantity15.value;
var quantity16 = +document.AddPO.quantity16.value;
var quantity17 = +document.AddPO.quantity17.value;
var quantity18 = +document.AddPO.quantity18.value;
var quantity19 = +document.AddPO.quantity19.value;
var quantity20 = +document.AddPO.quantity20.value;

 

will this work?

for (var i=1; i<21; i++) {
var quantity[i] = +document.AddPO.quantity[i].value;
}

 

thanks a lot.

Link to comment
https://forums.phpfreaks.com/topic/270775-how-to-shorten-this-js-code/
Share on other sites

No but

window["quantity" + i] = parseInt(document.AddPO["quantity" + i].value);

should. Works because (1) the global scope is actually the window object and (2) objects and arrays are mostly interchangeable.

Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.