I am having some problems with finding out if the value is missing or not.
I have the following select box on my screen:
<select id="user_list" onclick="load_user(this)" name="user_list" size="21" style="width:200px;">
<option value="1">Item1</option>
<option value="2">Item2</option>
<option value="4">Item4</option>
</select>
I am using the following JavaScript:
var userList = document.getElementById("user_list");
for (var i=0;i<userList.options.length; i++) {
if (userList.options[i].value != (i+1)) {
alert((i+1)+" is missing");
break;
}
else
{
alert((userList.options.length+1)+" is missing");
break;
}
}
The problem I am facing is that if option values 1,2,3,4 are there is replys back with 5 (as that is the next number) which is fine as that is what I want.
If I remove option 3 (like in my select box above) it keeps replying with 4 is missing when it should say 3 is missing.
If i remove the following code:
else
{
alert((userList.options.length+1)+" is missing");
break;
}
it does what I want it to do, but it doesn’t get the next value number after checking all values (so it stops after reading the last option in the list).
What it should do is go though all the options in the select box and if one of the values is missing it should alart the one that is missing. If all value numbers are there then it should alart the next available option that is missing.
Can anyone see what I am doing wrong?
It keeps telling you that
4is missing because you are running theelsecode in every iteration of the loop (until it finds one that is missing). The code in yourelseshould only be ran once. You can use a flag variable to set if one is missing and check after the loop. Try changing the js code to