1

I have this code :

<script>
  function getgroup(){

        var i=0;
        var total=document.getElementById("selectedOptions").length;

    while (i<total)
  {
    var group=document.getElementById("selectedOptions").value;
    var group2=group.substring(2);

    alert(group2);
     i++;
}
}
</Script>

I want to loop inside the list and get the value of each item in the list. By using this code I am getting only the value of the first item only.

Any help please?

1
  • There should only exist one object with a unique id. It's obvious there will be only one item. Commented Apr 10, 2012 at 13:16

2 Answers 2

2

i think u are using getElementById() method and by specification the ids of elements should be unique so the length will always be one. try adding a class and use getElementsByClass()

Sign up to request clarification or add additional context in comments.

2 Comments

Given that the element with id "selectedOptions" is a select, length will contain the number of options in that select.
but here nowhere it is mentioned.. i know this.. anyways thankx
1

You are assigning the value of the select element to the group variable. You need to loop through the options instead:

group = document.getElementById("selectedOptions").options[i].value;

6 Comments

Just a small thing, I want to sum() the group value, I did var group3=group3+group2; but it is not calculating
What is the result you're getting? You might have to use parseInt on the variables to be able to sum them up.
var group2=parseInt(group.substring(2)); I got NaN as a result
What kind of values do you have on your options? They have to be numbers of some kind if you want to add them together, which they are not if you get a NaN result.
Example1: G.132, I am getting the number (132) - Example2: G.125 and so on
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.