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 August 6th, 2004, 09:42 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
"Has no properties" - Dynamic building errors

Hey all,

I've run into a bit of a snag here with some JS code I'm working on... For the life of me, I can't seem to figure out what the problem is, and I have a feeling it's something obvious I'm missing. Isn't that usually the case with these things?

What I'm doing is dynamically adding <option> tags to a dropdown when a page loads using Javascript. The values for the options are being retrieve from a DB, and are dynamically added using a combination of JSP and Javascript.

Here's the function that adds the options:

Code:
    function addDate(date){
      d = document;
      dateSel = d.getElementsByName("startDate");
      opt = d.createElement("option");
      opt.value = date;
      opt.text = date;
      dateSel.item(0).appendChild(opt);
    }


The odd thing here is that when I call the function within the <body> tag's "onload" event, it will successfully add the new option:

Code:
<body onLoad="addDate('03JUL04');addDate('04JUL04');">


The problem I run into is when calling the addDate() function from within the body:

Code:
<script type="text/javascript">
  // <![CDATA[          
              addDate("03JUL04");            
  // ]]>            
</script>


I receive this error: Error: dateSel.item(0) has no properties.

Any ideas on why it's not working this way? Any help would be greatly appreciated.


Thanks,
Frank
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #2  
Old August 6th, 2004, 10:38 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
I would have figured you'd use getElementById to get the select and then append a child directly to the element. When are you calling the script within the body? If the select hasn't been rendered yet, I'd expect to see this error. It could also be that you can't manipulate the DOM like this until the whole document has loaded (body onload happens after everything's loaded, I believe), and inline scripts are executed before the document's fully loaded.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old August 6th, 2004, 10:38 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 16
Send a message via ICQ to dhouston
Good to see you back, btw.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > "Has no properties" - Dynamic building errors


Developer Shed Advertisers and Affiliates


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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap