2-level combo box, 2nd level title text needed

Discussion in 'JavaScript' started by jacka, Jul 30, 2007.

  1. #1
    Hi

    I am using the 2-level combo box which works very well.

    I now to later the code to display a place holder text in the 2nd level, and since I am a javascript begginer will take me ages to do.

    Please let me explain:
    This is the standard javascript which i use from here:

    http://www.javascriptkit.com/script/script2/2levelcombo.shtml

    The way it works at the moment is that the first line of text, in the list, is "select a catagory" which is reserved line to conatin COMBO title.
    Now when a selection is made from the list , a second list of catgories is displayed.

    What I would like to change is, similar to the first stage of the list, The second catagory also say "please select a catagory" and then display the available options from the 2nd line down.

    I have searched the forum for similar posting but have not found any.
    Your help is much appreciated.
    here is the standard code:

    <form name="dynamiccombo">
    <select name="stage2" size="1" onChange="displaysub()">
    <option value="#">This is a place Holder text </option>
    <option value="#">This is a Place Holder text </option>
    <option value="#">This is a Place Holder text </option>
    <option value="#">This is a Place Holder text </option>
    </select>
    <input type="button" name="test" value="Go!"
    onClick="gothere()">
    </form>
    
    <script>
    <!--
    
    //2-level combo box script- by javascriptkit.com
    //Visit JavaScript Kit (http://javascriptkit.com) for script
    //Credit must stay intact for use
    
    //STEP 1 of 2: DEFINE the main category links below
    //EXTEND array as needed following the laid out structure
    //BE sure to preserve the first line, as it's used to display main title
    
    var category=new Array()
    category[0]=new Option("SELECT A CATEGORY ", "") //THIS LINE RESERVED TO CONTAIN COMBO TITLE
    category[1]=new Option("Webmaster sites", "combo1")
    category[2]=new Option("News sites", "combo2")
    category[3]=new Option("Entertainment", "combo3")
    
    //STEP 2 of 2: DEFINE the sub category links below
    //EXTEND array as needed following the laid out structure
    //BE sure to preserve the LAST line, as it's used to display submain title
    
    var combo1=new Array()
    combo1[0]=new Option("JavaScript Kit","http://javascriptkit.com")
    combo1[1]=new Option("Dynamic Drive","http://www.dynamicdrive.com")
    combo1[2]=new Option("Freewarejava.com","http://www.freewarejava.com")
    combo1[3]=new Option("Free Web Templates","http://www.freewebtemplates.com")
    combo1[4]=new Option("Web Monkey","http://www.webmonkey.com")
    combo1[5]=new Option("BACK TO CATEGORIES","")   //THIS LINE RESERVED TO CONTAIN COMBO SUBTITLE
    
    var combo2=new Array()
    combo2[0]=new Option("CNN","http://www.cnn.com")
    combo2[1]=new Option("MSNBC","http://www.msnbc.com")
    combo2[2]=new Option("BBC News","http://news.bbc.co.uk")
    combo2[3]=new Option("ABC News","http://www.abcnews.com")
    combo2[4]=new Option("BACK TO CATEGORIES","")   //THIS LINE RESERVED TO CONTAIN COMBO SUBTITLE
    
    var combo3=new Array()
    combo3[0]=new Option("Hollywood.com","http://www.hollywood.com")
    combo3[1]=new Option("MTV","http://www.mtv.com")
    combo3[2]=new Option("ETOnline","http://etonline.com")
    combo3[3]=new Option("BACK TO CATEGORIES","")   //THIS LINE RESERVED TO CONTAIN COMBO SUBTITLE
    
    var curlevel=1
    var cacheobj=document.dynamiccombo.stage2
    
    function populate(x){
    for (m=cacheobj.options.length-1;m>0;m--)
    cacheobj.options[m]=null
    selectedarray=eval(x)
    for (i=0;i<selectedarray.length;i++)
    cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
    cacheobj.options[0].selected=true
    
    }
    
    function displaysub(){
    if (curlevel==1){
    populate(cacheobj.options[cacheobj.selectedIndex].value)
    curlevel=2
    }
    else
    gothere()
    }
    
    
    function gothere(){
    if (curlevel==2){
    if (cacheobj.selectedIndex==cacheobj.options.length-1){
    curlevel=1
    populate(category)
    }
    else
    location=cacheobj.options[cacheobj.selectedIndex].value
    }
    }
    
    //SHOW categories by default
    populate(category)
    
    //-->
    </script>
    
    <p align="center"><font face="arial" size="-2">This free script provided by</font><br>
    <font face="arial, helvetica" size="-2"><a href="http://javascriptkit.com">JavaScript
    Kit</a></font></p>
    HTML:

    many thanks
    jacka
     
    jacka, Jul 30, 2007 IP
  2. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi

    I can simply place a text in the first line of every combo box , such as this:
    combo1[0]=new Option("please select a catagory","")
    HTML:
    and
    combo2[0]=new Option("please select a catagory","")
    HTML:
    etc.
    But when I do this, any one who chooses that option by mistake and clicks the go button, gets taken back to the previous stage.
    All I want to do is to say:
    a)If you are in level 2
    b) and you choose first option
    c) nothing happens
    i,e. just populate level 2 again or take no action ( same as the first option in level 1)

    thanks for reading my posting.
    jacka
     
    jacka, Jul 31, 2007 IP
  3. jacka

    jacka Peon

    Messages:
    165
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi all

    I have added this code in the fucnction gothere() section which works ok, but I am not sure if it is correct.

    Can some one please confirm if it is legal statement.

    function gothere(){
    if (curlevel==2){
    if (cacheobj.selectedIndex==cacheobj.options.length-1){
    curlevel=1
    populate(category)
    
    }
    [COLOR="Red"]if (cacheobj.selectedIndex==0){
    
    
    }[/COLOR]
    
    else
    location=cacheobj.options[cacheobj.selectedIndex].value
    }
    }
    HTML:
    ie. when I click on teh first option in the 2nd level, which is a text message saying "please chhose a size", it does nothing which is what I want.

    thx
    jacka
     
    jacka, Aug 1, 2007 IP
  4. Askano

    Askano Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi everyone, I have tried to use this nice script as well.

    I was wondering if it could escape from the 2 level combobox and be a unlimited level combobox but with the cabability not to have to reach the last level in order to use the location. but already from the very first level.
    Lets say i have two subcategories and two links in the first level, then three subcategories and one link .. etc etc ...

    it would be awsome it that could be possible!

    I am not showing what I have done so far because nothing really worked. Neither chaning the "function displaysub()" by adding another level nor the "function gothere()"

    If I only had the way to add one more level and how to enable location on every level I would be able to work my way up to the levels I need!

    I know my description of problem isnt the brightest, but after 4 days fiddeling around it is my very first attempt ever to ask for help from the internet.

    whoever read this, thank you for taking the time, thank you in case you have a solution and sorry for my teribble english.
     
    Askano, Jun 7, 2011 IP