Connecting Tech Pros Worldwide Forums | Help | Site Map

dynamic menu problem with onchange event

Newbie
 
Join Date: May 2007
Posts: 7
#1: May 22 '07
Hi, I am trying to make a dynamic menu using javascript and an xml file. The structure of the page (test.html) is in the code below. When I load up the page the drop-down list is there, so I select a value from the list and it passes that value to the function to get the appropriate data and it prints out the values (which it does just fine).

The thing is when I select an item from the drop-down list the other html disappears (even the doctype, and drop down list), and it just displays the values... This is all happening on the same page (test.html), so I'm not sure what is going on. Would using "document.write" in the script mess it up so it only display the output from the function? Any help is appreciated, thanks.

Expand|Select|Wrap|Line Numbers
  1. <doctype etc...>
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5. function initMenu(value) {
  6. .. code to parse xml file and print out values
  7. }
  8. </script>
  9. </head>
  10. <body>
  11. <form name="myForm">
  12. <select name="mySelect" onchange="initMenu(this.value)">
  13. <option value="Item1">Item 1</option>
  14. <option value="Item2">Item 2</option>
  15. <option value="Item3">Item 3</option>
  16. <option value="Item4">Item 4</option>
  17. </select>
  18. </form>
  19. </body>
  20. </html>
  21.  

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: May 23 '07

re: dynamic menu problem with onchange event


Heya, chobo. Welcome to TSDN!

Quote:

Originally Posted by chobo

Would using "document.write" in the script mess it up so it only display the output from the function?

When you use document.write, the script has to open the document for writing. If it is still loading, this isn't a problem b/c the document is still open.

On the other hand, once the document finishes loading, it is 'closed', and the only way to open it is to erase it and start over.

This would be why you're only seeing the output of your function.

What you'll want to do instead is to dynamically create a new element and insert it into the body of your document, or else alter the innerHTML property of a container element.
Newbie
 
Join Date: May 2007
Posts: 7
#3: May 24 '07

re: dynamic menu problem with onchange event


Thanks for the help and information! I got it to work by cocantenating the output to a variable and using the innerHTML property.
Reply