|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
"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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
Good to see you back, btw.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > "Has no properties" - Dynamic building errors |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|