function buildMenu(myMenu) { var i, menuItems = ''; var $menu = $('<div class="MainMenuItems" />'); for (i in menu1) { menuItems += menu1[i]; } var menuHTML = $menu.append(menuItems); $(myMenu).html(menuHTML); }
buildMenus('.MainMenu'); It's untested, but does something like this work for you? What does your 'menu1' variable look like? I'm assuming its an array of HTML string sets, like '<div>menu1</div>', '<div>menu2</div>' On Apr 8, 9:15 am, "jomowe...@gmail.com" <jomowe...@gmail.com> wrote: > Not sure the best way to do this, but I am trying to build a menu with > a function then to insert it into the dom with jquery. I have tried > bothe the methods below to no avail. All I am getting is "undefined" > where the new links should be. If anyone can tell me what I am doing > wrong, or give a little hint on a better way to code this, you would > save the day > > //The menu function > function buildMenu() > { > var i; > for (i in menu1) > { > var menuItem = menu1[i]; > document.write(menuItem); > } > > } > > // The jquery manipulation Attempt #1 > $(".MainMenu").html("<div class='MainMenuItems'>" + buildMenu() + "</ > div>"); > > // Attempt #2 > $(".MainMenu").html("<div class='MainMenuItems'><script>buildMenu();</ > script></div>");