Results 1 to 12 of 12

Thread: [RESOLVED] How to add a selected option to a drop down menu

  1. #1
    Join Date
    Apr 2006

    resolved [RESOLVED] How to add a selected option to a drop down menu

    Hello everybody,

    I have a javascript adding new options to a drop down select menu getting the information from the database. The code is as written below:
    function setOptions1() {
    var selbox2 = document.egitimgiris.alttipID;
    selbox2.options.length = 0;
    selbox2.options[selbox2.options.length] = new Option('Option1','22')
    selbox2.options[selbox2.options.length] = new Option('Option2','23')
    selbox2.options[selbox2.options.length] = new Option('Option3','24')
    selbox2.options[selbox2.options.length] = new Option('Option4','25')
    I want one of them to be added as selected. I will put an "if" check and if it the value of the option and the value in the database is equal I want that option to be added as "selected".

    Is this possible? If yes, how? Please help.

    Thanks and regards

  2. #2
    Join Date
    Jul 2008
    urbana, il
    i would prefer a loop, but here's a simple way to do what you ask:

    function setOptions1(match) {
      var selbox2 = document.egitimgiris.alttipID, X;
       selbox2.options.length = 0;
       selbox2.options[selbox2.options.length] = X=new Option('Option1','22')
       selbox2.options[selbox2.options.length] = X=new Option('Option2','23')
       selbox2.options[selbox2.options.length] = X=new Option('Option3','24')
       selbox2.options[selbox2.options.length] = X=new Option('Option4','25')
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  3. #3
    Join Date
    Apr 2006
    Hi again,

    Thanks for the information but I think I could not explain my problem. That options are also created with a PHP code and loop. Independently from Javascript variables etc., I will check if two database query values are equal. If yes, I want to write new option as selected. Otherwise keep going like

    selbox2.options[selbox2.options.length] = new Option('Option1','22')

    Is there a function like
    selbox2.options[selbox2.options.length] = new Option('Option1','22','selected')??


  4. #4
    Join Date
    Feb 2010

    you can do that if you override the Option function

    <select id="sel1"></select>
    <script type="text/javascript">
    function Option(txt, val, sel){
     var o = document.createElement('option');
     o.value = val;
      o.setAttribute('selected', 'selected');
     return o;
    var sel = document.getElementById('sel1');
    sel.appendChild(new Option('a', 'aaa'));
    sel.appendChild(new Option('b', 'aaa', false));
    sel.appendChild(new Option('c', 'aaa', true));
    sel.appendChild(new Option('d', 'aaa', false));
    sel.appendChild(new Option('e', 'aaa'));
    tried this in ie6 and ff

  5. #5
    Join Date
    Apr 2006
    Thanks Gozzy,

    I found another solution with the help of a friend. I will write that one also.

    var dbvalue = "25";  // or whatever
    for (var i =0; i <selbox2.options.length; i++) {
    if (selbox2[i].value == dbvalue) {
    selbox2.options[i].selected = true;
    This code updates the selected part of the option.

    Thanks anyway

  6. #6
    Join Date
    Mar 2010
    USA, KY

    Examples - take it from here

    Well, trying to figure out what you are doing. But PHP and JavaScript are 2 different beasts and have to be treated that way. For instance PHP does not have access to 'document.formName.fieldName.value' that is for javasciprt to parse. So in order to do the same thing in javascript and PHP you have to write it 2 different ways.

    function selectTheme(){
      $themeele = document.getElementById("theme");
    function chooseTheme(){
      $sel = document.createElement("select");
      $sele = $e.appendChild($sel);
      additem("blue","The Blues");
    function additem($val,$txt){
      $x = document.getElementById("theme");
      $opt = document.createElement("option");
      $opt.value = $val;
      $opt.text = $txt;
    /* you must set a span in the html to "chooseTheme" to start */
    /* <span id="chooseTheme">&nbsp;</span> */
    this creates a select box then uses the additem() call to add options to the list.
    then to use javascript to kinda do what you are talking about you would have to run through the options.values to find it that value is eq to the other one then mark it selected something like this:
    /* run through the options in the select box */
    for($x=0; $x <= document.f1.v1.options.length; $x++){
      /* if the text in the box matches the otherVal i pulled from the db earlier */
      if(documnet.f1.v1.options[$x].text == $otherVal){
        /* then make it marked */
        document.f1.v1.options[$x].selected = true;
    for javascript to work in this case you MUST pull the values out of the db before hand and you may want to stick them in hidden fields

    or the easy way in PHP
    PHP Code:
    /* grab the values from the database */
    $id1 mysql_fetch_object(mysql_query("select username from user where uid = '$uid'"));
    $id2 mysql_fetch_object(mysql_query("select username from active where uid = '$uid'"));
    /* start the select box */
    echo('<select id="usernames">');
    /* run through the list */
    /* and if they are the same mark it */
    if($id1 == $id2){
    ' selected');
    my php is a little rusty so this code should NOT work as it is but comes very close

  7. #7
    Join Date
    Apr 2006
    Simply what I am doing is reading data from the database and create javascript according to the data. By doing that I can change the content of a sub-category drop down list without refreshing the page. The rest of the javascript code is refreshing the sub-category drop down content on change of category drop down list. This one is the default values to come to the page load on the edit section of the products. So when I click on "Edit" link a page comes with a selected category drop down + a selected sub-category dropdown. But when I change the category drop down value. The content of the sub-category drop down content changes without refreshing the page.

    So I don't try to make PHP and Javascript send variables to each other or communicate. What I am doing is writing the content of a javascript using a PHP loop

  8. #8
    Join Date
    Mar 2010
    USA, KY
    ok based on that then what you should try is something like this (borrowing from the above code)
    HTML Code:
    /* grabs all TOP level categories */
    $cat = mysql_query("select name from category where parent = 1");
    /* grabs all cats that are really subcats */
    $subcat = mysql_query("select name from category where parent = 2");
    <script type="text/javascript">
    function chooseTheme(){
      $sel = document.createElement("select");
      $sele = $e.appendChild($sel);
        /* going back to php */
        while ($row = db_fetch_object($subcat)){
          /* outputs javascript for each subcat */
    There are javascripts out there on the net which dynamically create one select based on another select so you should try to grab one of those then cut it with the php code base on the above
    But what this does is grabs all categories and categories that belong to another category then loops through them rendering the javascript "additem()" calls.

  9. #9
    Join Date
    Mar 2010
    USA, KY
    oh sry use "mysql" where ever you see something like "db_fetch_object"
    i always use the "db_" prefix then load in a script that translates that to whatever database you use.

  10. #10
    Join Date
    Mar 2010
    USA, KY
    check out http://dcs777.homeip.net/sites/defau...ryChooser.html for a script that came from javascript.com for examples on how to update one select based on the other

  11. #11
    Join Date
    Mar 2010
    USA, KY
    sry that script came from http://javascriptsource.com/ gotta give credit where credit is due

  12. #12
    Join Date
    Apr 2006
    dewcansam thanks for all entries but on one of my previous messages I explained that I found the solution. My code works perfectly. Thank you anyway

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.20158 seconds
  • Memory Usage 2,983KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (6)bbcode_code
  • (1)bbcode_html
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (12)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (12)postbit
  • (12)postbit_onlinestatus
  • (12)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • bbcode_parse_start
  • postbit_imicons
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates