/    Sign up×
Community /Pin to ProfileBookmark

Dynamic multiple menus

I have an application with a parent menu and a dependent child menu (actually several of them). Both are populated from a mySQL database (code not shown). Php directly writes the parent options in HTML when the page is first loaded (get_parent_menu). When the parent menu is changed, the onChange Javascript (update_child) updates the child menu. Works exactly as described, except I really want the child menu to be populated when the pages is loaded, and not wait until the parent menu is changed. Is there a way to have update_child run when the page is first loaded? Ideally, I wouldn’t use a onload located outside of the parent select html which wouldn’t allow “this” and prevent the code from being modular. Thanks

<SELECT name=”menu_1″ id=”menu_1″ onChange=”update_child( this.options[ this.selectedIndex ] );”>
<?php
include_once ‘functions.php’;
echo get_parent_menu(“SQL_menu_1”);
?>
</SELECT>

<SELECT name=”menu_1_children” id=”menu_1_children”></SELECT>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gphJan 02.2006 — onload=function(){

document.getElementById('menu_1').onchange()

}
Copy linkTweet thisAlerts:
@NotionCommotionauthorJan 02.2006 — Would your proposed code be located in body tags (i.e. <body onload= function() { document.getElementById('menu_1').onchange() }>) or within the select tags, or somewhere else? Also, can you elaborate a little on what it does? Thanks
Copy linkTweet thisAlerts:
@gphJan 02.2006 — It fires the onchange() event on the select element. So that whatever is assigned to the select onchange event will execute.

You can put it in script tags anywhere in the page, commonly in the head, like this
<i>
</i>&lt;script type="text/javascript"&gt;

onload=function(){
document.getElementById('menu_1').onchange()
}

&lt;/script&gt;

or in the body tag like this
<i>
</i>&lt;body onload="document.getElementById('menu_1').onchange()"&gt;

personally I never use the body tag, I don't like scripts scattered throughout the document.
×

Success!

Help @NotionCommotion spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...