needs_upgrade Posted November 16, 2012 Share Posted November 16, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270775-how-to-shorten-this-js-code/ Share on other sites More sharing options...
requinix Posted November 16, 2012 Share Posted November 16, 2012 (edited) 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 November 16, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/270775-how-to-shorten-this-js-code/#findComment-1392871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.