Solved

Select Chosen Option in Menu

Posted on 2004-11-01
2,528 Views
Last Modified: 2008-01-09
I have the following script set up so that one someone choses an option from a menu it passes through variables, but it does not "select" that option that was chosen. Here is my code so far:

<html>
<head>
<title>Test</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
   eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
   if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>
<form name="selectMonth">
<select name="menu" id="menu" onChange="MM_jumpMenu('self',this,1)">
<option value="monthcal.dll?BeginDate=10/1/04&EndDate=12/31/05">October 2004</option>
<option value="monthcal.dll?BeginDate=11/1/04&EndDate=12/31/05">November 2004</option>
<option value="monthcal.dll?BeginDate=12/1/04&EndDate=12/31/05">December 2004</option>
<option value="monthcal.dll?BeginDate=1/1/05&EndDate=12/31/05">Janurary 2005</option>
<option value="monthcal.dll?BeginDate=2/1/05&EndDate=12/31/05">February 2005</option>
</select>
</form>
</body>
</html>

Let's say someone choses November 2004. I want the November 2004 option to appear like:

<option value="monthcal.dll?BeginDate=11/1/04&EndDate=12/31/05" selected>November 2004</option>

I need to be able to do this with javascript only, if that is possible.
0
Question by:thecode101
    12 Comments
     
    LVL 25

    Expert Comment

    by:devic
    change the line:
    <select name="menu" id="menu" onChange="MM_jumpMenu('self',this,1)">
    to:
    <select name="menu" id="menu" onChange="MM_jumpMenu('self',this,0)">
    0
     
    LVL 25

    Expert Comment

    by:devic
    wait, you change location of the same page....
    0
     
    LVL 3

    Author Comment

    by:thecode101
    That change did not work. Yes when an option is chosen it reloads the page with different variables passed through.
    0
     
    LVL 25

    Expert Comment

    by:devic
    ok, try this:
    ==================
    <html>
    <head>
    <title>Test</title>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore)
    { //v3.0
          eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    }
    function setDef()
    {
          if(self.location.href.indexOf("?")!=-1)
          {
                alert(self.location.href.split("?")[1])
                document.selectMonth.menu.value="monthcal.dll?"+self.location.href.split("?")[1];
          }
    }
    //-->
    </script>
    </head>

    <body onload=setDef();>
    <form name="selectMonth">
    <select name="menu" id="menu" onChange="MM_jumpMenu('self',this,1)">
    <option value="monthcal.dll?BeginDate=10/1/04&EndDate=12/31/05">October 2004</option>
    <option value="monthcal.dll?BeginDate=11/1/04&EndDate=12/31/05">November 2004</option>
    <option value="monthcal.dll?BeginDate=12/1/04&EndDate=12/31/05">December 2004</option>
    <option value="monthcal.dll?BeginDate=1/1/05&EndDate=12/31/05">Janurary 2005</option>
    <option value="monthcal.dll?BeginDate=2/1/05&EndDate=12/31/05">February 2005</option>
    </select>
    </form>
    </body>
    </html>
    0
     
    LVL 3

    Author Comment

    by:thecode101
    That did not work either. There is no problem with passing the variables. The problem is that when November is clicked that option needs to be selected when the page is reloaded. If December is clicked that option needs to be selected when the page is reloaded.
    0
     
    LVL 25

    Expert Comment

    by:devic
    note you can not test this on your desktop, only on server:
    if you want test it on desktop,

    then change name of file to: test.html AND replace all monthcal.dll to test.html

    EXAMPLE:

    ============= test.html ===============
    <html>
    <head>
    <title>Test</title>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore)
    { //v3.0
          eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    }
    function setDef()
    {
          if(self.location.href.indexOf("?")!=-1)
          {
                document.selectMonth.menu.value="test.html?"+self.location.href.split("?")[1];
          }
    }
    //-->
    </script>
    </head>

    <body onload=setDef();>
    <form name="selectMonth">
    <select name="menu" id="menu" onChange="MM_jumpMenu('self',this,1)">
    <option value="test.html?BeginDate=10/1/04&EndDate=12/31/05">October 2004</option>
    <option value="test.html?BeginDate=11/1/04&EndDate=12/31/05">November 2004</option>
    <option value="test.html?BeginDate=12/1/04&EndDate=12/31/05">December 2004</option>
    <option value="test.html?BeginDate=1/1/05&EndDate=12/31/05">Janurary 2005</option>
    <option value="test.html?BeginDate=2/1/05&EndDate=12/31/05">February 2005</option>
    </select>
    </form>
    </body>
    </html>
    0
     
    LVL 3

    Author Comment

    by:thecode101
    I am testing this on a server, and yes I renamed the page for testing purposes. It is not working. When the page is reloaded, and lets say for example December is chosen the html code should changed to:

    <option value="test.html?BeginDate=12/1/04&EndDate=12/31/05" select>December 2004</option>

    instead of

    <option value="test.html?BeginDate=12/1/04&EndDate=12/31/05">December 2004</option>

    So that December will be highlighted an appeared default selected.
    0
     
    LVL 25

    Accepted Solution

    by:
    ach, I got it, you need the color ;)

    ====================================
    <html>
    <head>
    <title>Test</title>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_jumpMenu(targ,selObj,restore)
    { //v3.0
        eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    }
    function setDef()
    {
        if(self.location.href.indexOf("?")!=-1)
        {
            document.selectMonth.menu.focus();
                document.selectMonth.menu.value="test.html?"+self.location.href.split("?")[1];
        }
    }
    //-->
    </script>
    </head>

    <body onload=setDef();>
    <form name="selectMonth">
    <select name="menu" id="menu" onChange="MM_jumpMenu('self',this,1)">
    <option value="test.html?BeginDate=10/1/04&EndDate=12/31/05">October 2004</option>
    <option value="test.html?BeginDate=11/1/04&EndDate=12/31/05">November 2004</option>
    <option value="test.html?BeginDate=12/1/04&EndDate=12/31/05">December 2004</option>
    <option value="test.html?BeginDate=1/1/05&EndDate=12/31/05">Janurary 2005</option>
    <option value="test.html?BeginDate=2/1/05&EndDate=12/31/05">February 2005</option>
    </select>
    </form>
    </body>
    </html>
    0
     
    LVL 3

    Author Comment

    by:thecode101
    Still not working, I am using Firefox 1.0 and IE 6.0. When you test it and chose an option, when the page reloads is that option you just selected the default option in the select menu?
    0
     
    LVL 25

    Expert Comment

    by:devic
    http://home.arcor.de/athens/expexc/js/String/test.html

    IE: selected
    FireFox: selected without hilighting

    and what do you see?
    0
     
    LVL 3

    Author Comment

    by:thecode101
    Well yours worked and when I copied it exactly it worked for me. I don't know what I was doing wrong, but I am glad it is working. Thanks for your help.
    0
     
    LVL 25

    Expert Comment

    by:devic
    thank you too!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now