JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 12th, 2006, 05:45 PM
talkingfishie talkingfishie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: marquette
Posts: 2 talkingfishie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 51 sec
Reputation Power: 0
Javascript for loop problem.

a page that im working on loads a table with information from a database using xajax. i have the number of items in the table and each item has a unique id but when i go to process the changes the user makes i get the error "document.getElementById("upc_" + i) has no properties". thought if i remove the for loop i get the first upc and no error. below is my javascript function.


function insert_upc()
{
var num = document.getElementById("num_rows").value;

var i = 0;
for(i=0;i<=num;i++)
{
var upc = document.getElementById("upc_" + i).value;

document.getElementById("list").innerHTML = i;
//xajax_import_upcs(id, upc);
}
}


<tr bgcolor="#ededed">
<td rowspan="2"><input name="checkbox" value="checkbox" checked="checked" type="checkbox"></td>
<td>SKU: B111 ---- Color: Midnight ---- Size: 34 X 30</td>
<td rowspan="2">035481266126<input value="2056165" id="id_80" type="hidden"><input value="035481266126" id="upc_80" type="hidden"></td>
</tr>

Reply With Quote
  #2  
Old June 12th, 2006, 06:17 PM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 135 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 45 m 13 sec
Reputation Power: 4
Are all of the numbers in the IDs sequential?

You could try this:
Code:
 function insert_upc()
{
  var num = document.getElementById("num_rows").value;

  var i = 0;
  for(i=0;i<=num;i++)
  {
    if(document.getElementById("upc_" + i))
    {
      var upc = document.getElementById("upc_" + i).value;

      document.getElementById("list").innerHTML = i;
      //xajax_import_upcs(id, upc);
    }
  }
}

Reply With Quote
  #3  
Old June 13th, 2006, 12:38 AM
ravs ravs is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: gurgaon, haryana, india
Posts: 60 ravs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 44 m 29 sec
Reputation Power: 3
Send a message via Yahoo to ravs
i still see that it will not work


because
Code:

var num = document.getElementById("num_rows").value;

the num is not parsed in integer

do like this


Code:

var num = parseInt(document.getElementById("num_rows").value);
Comments on this post
Kravvitz agrees: Good catch.

Reply With Quote
  #4  
Old June 13th, 2006, 12:52 AM
Kravvitz Kravvitz is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: USA
Posts: 135 Kravvitz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 h 45 m 13 sec
Reputation Power: 4
Specifying the second argument will keep the number from being treated as octal if the number ever has a leading zero.
Code:
var num = parseInt(document.getElementById("num_rows").value,10);

Reply With Quote
  #5  
Old June 13th, 2006, 09:42 AM
talkingfishie talkingfishie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2006
Location: marquette
Posts: 2 talkingfishie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 51 sec
Reputation Power: 0
problem solved

thanks alot for the help. the parseInt fixed the problem.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > Javascript for loop problem.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT