Show/Hide Layers based on form option

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • veganeater

    #1

    Show/Hide Layers based on form option

    Hi,

    I'm wondering if there's a way that I can select which <div> to show based
    on the user's selection from a dropdown/listbox form.

    <form name="form1" method="post" action="">
    <select name="internet" >
    <optgroup label="Network" >
    <option label="option1" >Internet</option>
    </optgroup>
    <optgroup label="Access">
    <option label="option2. 0">Portal</option>
    <option label="option2. 1">Webmail</option>
    <option label="option2. 2">POP3 Email</option>
    <option label="option2. 3">Messenger </option>
    <option label="option2. 4">Newsgroup s</option>
    </optgroup>
    </select>
    </form>

    Is there a way that I can have a particular <div> show based on a selection
    from this list? I know that you can not directly apply actions to the
    options, so it presumably there has to be a javascript method of doing it...
    If anyone can point me in the right direction, it would be greatly
    appreciated.

    Thanks in advance!

    |veganeater|

    // my apologies for the xpost on comp.lang.java. javascript


  • Dag Sunde

    #2
    Re: Show/Hide Layers based on form option

    "veganeater " <veganeater.nos pammers@gmail.c om> wrote in message
    news:2vadnTdFrc AaH3DcRVn-ug@rogers.com.. .[color=blue]
    > Hi,
    >
    > I'm wondering if there's a way that I can select which <div> to show based
    > on the user's selection from a dropdown/listbox form.
    >
    > <form name="form1" method="post" action="">
    > <select name="internet" >
    > <optgroup label="Network" >
    > <option label="option1" >Internet</option>
    > </optgroup>
    > <optgroup label="Access">
    > <option label="option2. 0">Portal</option>
    > <option label="option2. 1">Webmail</option>
    > <option label="option2. 2">POP3 Email</option>
    > <option label="option2. 3">Messenger </option>
    > <option label="option2. 4">Newsgroup s</option>
    > </optgroup>
    > </select>
    > </form>
    >
    > Is there a way that I can have a particular <div> show based on a[/color]
    selection[color=blue]
    > from this list? I know that you can not directly apply actions to the
    > options, so it presumably there has to be a javascript method of doing[/color]
    it...[color=blue]
    > If anyone can point me in the right direction, it would be greatly
    > appreciated.
    >[/color]

    the label attribute for the option element is not in use by any browser
    I know of...

    Here is a crude example:

    <head>
    <meta http-equiv="content-type" content="text/html;charset=is o-8859-1">
    <title>TestPage </title>

    <script type="text/javascript">
    function internetSelect( ) {
    var selectedValue = document.getEle mentById("inter net").value;
    document.getEle mentById("opt10 ").style.visibi lity = "hidden";
    document.getEle mentById("opt20 ").style.visibi lity = "hidden";
    document.getEle mentById("opt21 ").style.visibi lity = "hidden";
    document.getEle mentById("opt22 ").style.visibi lity = "hidden";
    if(selectedValu e == "option10")
    document.getEle mentById("opt10 ").style.visibi lity = "visible";
    if(selectedValu e == "option20")
    document.getEle mentById("opt20 ").style.visibi lity = "visible";
    if(selectedValu e == "option21")
    document.getEle mentById("opt21 ").style.visibi lity = "visible";
    if(selectedValu e == "option22")
    document.getEle mentById("opt22 ").style.visibi lity = "visible";
    }
    </script>
    </head>

    <body onload="interne tSelect();">
    <select id="internet" onchange="inter netSelect();">
    <optgroup label="Network" >
    <option value="option10 ">Internet</option>
    </optgroup>
    <optgroup label="Access">
    <option value="option20 ">Portal</option>
    <option value="option21 ">Webmail</option>
    <option value="option22 ">POP3 Email</option>
    </optgroup>
    </select>

    <div id="opt10" style="backgrou nd-color:#00FFFF; visibility:hidd en;
    border: 1px solid black;">
    <h2>Internet</h2>
    </div>
    <div id="opt20" style="backgrou nd-color:#FFFF00; visibility:hidd en;
    border: 1px solid black;">
    <h2>Portal</h2>
    </div>
    <div id="opt21" style="backgrou nd-color:#FF99FF; visibility:hidd en;
    border: 1px solid black;">
    <h2>Webmail</h2>
    </div>
    <div id="opt22" style="backgrou nd-color:#9999FF; visibility:hidd en;
    border: 1px solid black;">
    <h2>POP3 Email</h2>
    </div>


    </body>
    </html>


    --
    Dag.


    Comment

    • RobB

      #3
      Re: Show/Hide Layers based on form option

      veganeater wrote:[color=blue]
      > Hi,
      >
      > I'm wondering if there's a way that I can select which <div> to show[/color]
      based[color=blue]
      > on the user's selection from a dropdown/listbox form.
      >
      > <form name="form1" method="post" action="">
      > <select name="internet" >
      > <optgroup label="Network" >
      > <option label="option1" >Internet</option>
      > </optgroup>
      > <optgroup label="Access">
      > <option label="option2. 0">Portal</option>
      > <option label="option2. 1">Webmail</option>
      > <option label="option2. 2">POP3 Email</option>
      > <option label="option2. 3">Messenger </option>
      > <option label="option2. 4">Newsgroup s</option>
      > </optgroup>
      > </select>
      > </form>
      >
      > Is there a way that I can have a particular <div> show based on a[/color]
      selection[color=blue]
      > from this list? I know that you can not directly apply actions to the
      > options, so it presumably there has to be a javascript method of[/color]
      doing it...[color=blue]
      > If anyone can point me in the right direction, it would be greatly
      > appreciated.
      >
      > Thanks in advance!
      >
      > |veganeater|
      >
      > // my apologies for the xpost on comp.lang.java. javascript[/color]

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
      "http://www.w3.org/TR/html4/strict.dtd">

      <html>
      <head>
      <title>untitled </title>
      <style type="text/css">

      ..showhide {
      width: 130px;
      height: 16px;
      font-weight: 800;
      text-align: center;
      border: 2px black dashed;
      display: none;
      }

      </style>
      <script type="text/javascript">

      var divs = {
      'option1' : 'InternetDIV' ,
      'option2.0' : 'PortalDIV' ,
      'option2.1' : 'WebmailDIV' ,
      'option2.2' : 'POP3DIV' ,
      'option2.3' : 'MessengerDIV' ,
      'option2.4' : 'NewsgroupsDIV'
      }

      function showhide(obj)
      {
      var el, v = obj.options[obj.selectedInd ex].value;
      for (var opt in divs)
      if (el = document.getEle mentById(divs[opt]))
      el.style.displa y = (opt != v) ? 'none' : 'block';
      }

      window.onload = function()
      {
      document.getEle mentById('inter net').onchange( );
      }

      </script>
      </head>
      <body>
      <form name="form1" method="post" action="">
      <select id="internet" name="internet" onchange="showh ide(this)">
      <optgroup label="Network" >
      <option value="option1" selected="selec ted">Internet</option>
      </optgroup>
      <optgroup label="Access">
      <option value="option2. 0">Portal</option>
      <option value="option2. 1">Webmail</option>
      <option value="option2. 2">POP3 Email</option>
      <option value="option2. 3">Messenger </option>
      <option value="option2. 4">Newsgroup s</option>
      </optgroup>
      </select>
      </form>
      <div id="InternetDIV " class="showhide ">Internet</div>
      <div id="PortalDIV" class="showhide ">Portal</div>
      <div id="WebmailDIV " class="showhide ">Webmail</div>
      <div id="POP3DIV" class="showhide ">POP3 Email</div>
      <div id="MessengerDI V" class="showhide ">Messenger </div>
      <div id="NewsgroupsD IV" class="showhide ">Newsgroup s</div>
      </body>
      </html>

      Comment

      • veganeater

        #4
        Re: Show/Hide Layers based on form option

        Wow, thanks guys!

        I was concerned that it would be far more difficult then it apparently is -
        or as you made it look.

        Dag Sunde - I understand your concern about the use of the label descriptor
        on the options tag being supported on all browsers. Unfortunalty - and much
        to my shgrin - I'm designing a small application that is intended to run
        /only/ on MSIE6+. Kind of a pain as I, myself, use Opera... oh, well...

        Anyhow, thanks again!

        |veganeater|


        Comment

        Working...