473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Whats the wrong with this for loop?

228 100+
Hi,

i am trying to pass text from one select box to another select box. The logic is if 10 are added, no more passing must happen. Also if an item is already added, it mustn't be added again. I am using the for loop to check the existence of an item but it is not working: what am i doing wrong?

Expand|Select|Wrap|Line Numbers
  1. function PassSelectValues(){
  2. //pass values from select boxes to select boxes
  3. var counter;
  4. var select = document.getElementById("addedapples");//add to this
  5. var srcselect_id = document.getElementById("srcapples");//id of the src select box
  6. var srcselect_name= srcselect_id.options[srcselect_id.selectedIndex].text;
  7. var srcselect_code= srcselect_id.options[srcselect_id.selectedIndex].value;
  8. var ttladded=select.options.length;//how many apples had been added already?
  9. alert(ttladded);
  10. //is the item already added?
  11. if(ttladded==10){
  12.  alert("10 apples already passed");
  13.              return false;    
  14. }
  15. if(ttladded==0){
  16.      select.options[select.options.length] = new Option(srcselect_name, srcselect_code);
  17.      }
  18.  
  19. for(counter=0;counter<ttladded;counter++){
  20. var existvalue=select.options[counter].value;
  21.  
  22.      if(existvalue==srcselect_code){
  23.  
  24.         alert("Item is already in the list");
  25.  
  26.         break;
  27.         return;
  28.           }// if(existvalue==srcselect_code){
  29.  
  30.     else if(existvalue!=srcselect_code){//add it
  31.  
  32.       select.options[select.options.length] = new Option(srcselect_name, srcselect_code);
  33.     }//existvalue!==srcselect_code
  34.  
  35.  
  36.     }//end for
  37.  
  38.  
  39.  
  40. }//end of function
  41.  
why isn't counter incrementing at all? The alert message box does appear saying item exists but the item gets added anyway.
Aug 29 '10 #1
2 1243
gits
5,390 Expert Mod 4TB
it is a quite obvious mistake - you loop through all existing options to check whether a node should be appended. you always start at option 0 so it might be that the option is added and the condition fires on the next item to check ... so you might move the add-code outside of the loop and use a flag instead like this:

Expand|Select|Wrap|Line Numbers
  1. function PassSelectValues() {
  2.     var counter;
  3.     var select         = document.getElementById("addedapples");
  4.     var srcselect_id   = document.getElementById("srcapples");
  5.     var srcselect_name = srcselect_id.options[srcselect_id.selectedIndex].text;
  6.     var srcselect_code = srcselect_id.options[srcselect_id.selectedIndex].value;
  7.  
  8.     var ttladded = select.options.length;
  9.  
  10.     var optionToAdd = new Option(
  11.         srcselect_name, srcselect_code
  12.     );
  13.  
  14.     if(ttladded == 10) {
  15.         alert("10 apples already passed");
  16.         return;    
  17.     }
  18.  
  19.     if(ttladded == 0) {
  20.         select.options[select.options.length] = optionToAdd;
  21.     }
  22.  
  23.     var doNotAdd = false;
  24.  
  25.     for(counter = 0; counter < ttladded; counter++) {
  26.         var existvalue = select.options[counter].value;
  27.  
  28.         if(existvalue == srcselect_code) {
  29.             alert("Item is already in the list");
  30.             doNotAdd = true;
  31.             break;
  32.         } 
  33.     }
  34.  
  35.     if (!doNotAdd) {
  36.        select.options[select.options.length] = optionToAdd;
  37.     }
  38. }
  39.  
Aug 30 '10 #2
samvb
228 100+
thank you!

you saved my day big time!
Sep 13 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
5
by: Alexandre Martins | last post by:
Provider=Microsoft.Jet.OLEDB.4.0;UserId=Admin;Password=teste;Data Source=C:\Inetpub\wwwroot\inktoner\dados\db_inktoner.mdb;Persist Security Info=True I can't connect in my database ! whats...
1
by: aa | last post by:
When I am reading from local disk (d:), everithing is OK, but then I am reading from map disk I am geting the this error. Whats wrong. Thanks Server Error in '/Extra' Application....
3
by: mahsa | last post by:
Hi do you know whats wrong with this code? <asp:HyperLink id="HLink_Help" runat="server" NavigateUrl='<%# "javascript:window.open('comments.aspx?id=1,width=500,height=600, scrollBars=yes');"...
1
by: '~=_Slawek_=~' | last post by:
$DOW = (jddayofweek(unixtojd(mktime(1, 1, 1, $month, $day, $year)))+6)%7; $DOW= (jddayofweek(juliantojd($month, $day, $year))+6)%7; The results are supposed to be the same, but they are not....
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
5
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
1
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
5
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.