-3
a[0]=parseFloat(document.getElementById("select18").value);
a[1]=parseFloat(document.getElementById("select19").value);
a[2]=parseFloat(document.getElementById("select20").value);
a[3]=parseFloat(document.getElementById("select21").value);
a[4]=parseFloat(document.getElementById("select22").value);
a[5]=parseFloat(document.getElementById("select23").value);
a[6]=parseFloat(document.getElementById("select24").value);
a[7]=parseFloat(document.getElementById("select25").value);
a[8]=parseFloat(document.getElementById("select26").value);
a[9]=parseFloat(document.getElementById("select27").value);
a[10]=parseFloat(document.getElementById("select28").value);
a[11]=parseFloat(document.getElementById("select29").value);
a[12]=parseFloat(document.getElementById("select30").value);
a[13]=parseFloat(document.getElementById("select31").value);
a[14]=parseFloat(document.getElementById("select32").value);
a[15]=parseFloat(document.getElementById("select33").value);
a[16]=parseFloat(document.getElementById("select34").value);

I have done it like this. how to do it using loop? (small code please)

3
  • What have you tried, seems quite straight forward. Use a for-loop from 0 to 16, and have a[i] and ("select"+(i+18)). Commented Nov 7, 2015 at 6:15
  • you asked a very similar question and got a perfectly good answer which you accepted an hour ago - stackoverflow.com/questions/33579113/… .. Commented Nov 7, 2015 at 6:16
  • @JaromandaX It's not exactly the same question, but the same concept applies. Looks like the OP needs to look into how for-loops work in general. Commented Nov 7, 2015 at 6:17

2 Answers 2

1

try like this

var startNode=18;
var endNode=34;
var index=0;
var a=[];
for(var i=startNode;i<=endNode;i++){
  a.push(parseFloat(document.getElementById("select"+i).value));
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for for you answer
@jagdish you are welcome
0

setting an id to each element - isn't good, try to set some class to tham, and make something like this:

var selects = document.geteElementsByClassName('selectClass'),
    i = 0, len = selects.length,
    a = [];

for (i; i < len; i++) {
    a.push(selects[i].value);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.