Welcome to Dream.In.Code
Become an Expert!

Join 149,523 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,397 people online right now. Registration is fast and FREE... Join Now!




toggle two elements via a dropdown menue

 
Reply to this topicStart new topic

toggle two elements via a dropdown menue

mhewson
4 Jun, 2007 - 01:30 PM
Post #1

New D.I.C Head
*

Joined: 3 Jun, 2007
Posts: 3


My Contributions
I am trying to create a function that will allow me to swap the class names of two span tags (show/hide) with the use of a drop down form –

Here is the html

<li><a href="#name">Name</a>
<div class="toggleable">
<span class="show"><a href="mailto:default;">bal@.com</a></span>
<span class="hide">555-555-5555</span>
</div>
</li>
</ul>
<div id="toggle”>
<select>
<option value="email">Email address</option>
<option value="phone">Phone number</option>
</select>
</div>
Any ideas, the point would be to always have one visible but not to allow both at the same time?

User is offlineProfile CardPM
+Quote Post

Oaty
RE: Toggle Two Elements Via A Dropdown Menue
4 Jun, 2007 - 08:04 PM
Post #2

New D.I.C Head
*

Joined: 10 May, 2007
Posts: 28


My Contributions
Explanations are in the comments....
CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Toggle</title>
<style type="text/css">
<!--
.show {display:inline;}
.hide {display:none;}
-->
</style>
<script type="text/javascript">
<!--
function toggle(sel){
    // the whole select object is passed when an onchange event occurs
    // selectedIndex is the index of the option chosen in the select object
    var idx = sel.selectedIndex;    // idx is used as shorthand for selectedIndex        
    // First we loop through all the options and hide the ones not chosen                                
    for( opts = 0; opts < sel.options.length; opts++){
        if(sel.options[opts].value != sel.options[idx].value){
            // Since your span ids and option values are the same we use
            // the non-selected value to create our DOM object via getElementById
            hide_span = document.getElementById(sel.options[opts].value);
            hide_span.className = "hide";    // we reassign the DOM objects class
        }
    }
    // Now that we hid those that are not to be shown, we can show the span
    // that was selected.
    var show_span = document.getElementById(sel.options[idx].value);
    show_span.className = "show";
}
// -->
</script>
</head>
<body>
<ul>
<li><a href="#name">Name</a>
<div class="toggleable">
<span class="show" id="email"><a href="mailto:default;">bal@.com</a></span>
<span class="hide" id="phone">555-555-5555</span>
</div>
</li>
</ul>
<div id="toggle”>
<!-- in my IE7(Vista) w/o a form tag the options were displayed as text
As is the select was not there.
-->
<form action="" name="togglet">
<!-- Call the toggle function when the value of the select box is changed.
     Pass this (a reference to the select tag) as the only parameter
-->
<select onchange="toggle(this);" name="toggle">
    <option value="email">Email address</option>
    <option value="phone">Phone number</option>
</select>
</form>
</div>
</body>
</html>

User is offlineProfile CardPM
+Quote Post

mhewson
RE: Toggle Two Elements Via A Dropdown Menue
4 Jun, 2007 - 09:01 PM
Post #3

New D.I.C Head
*

Joined: 3 Jun, 2007
Posts: 3


My Contributions
Thanks! your page works great - I guess I needed to be more specific about how much code I wanted to alter
here is what my page looks like

CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Web Developer: Exercise</title>
  <style type="text/css">
        @import url("css/master.css");
    </style>
  <!--[if IE]>
<style type="text/css">
@import url("css/ie.css");
</style>
<![endif]-->
  <!--[if lt IE 7]>
<style type="text/css">@import url("css/ie6.css");</style>
<![endif]-->

  <script type="text/javascript" src="js/contact.js"></script>

  <script type="text/javascript">
<!--
function toggle(sel){
    // the whole select object is passed when an onchange event occurs
    // selectedIndex is the index of the option chosen in the select object
    var idx = sel.selectedIndex;    // idx is used as shorthand for selectedIndex        
    // First we loop through all the options and hide the ones not chosen                                
    for( opts = 0; opts < sel.options.length; opts++){
        if(sel.options[opts].value != sel.options[idx].value){
            // Since your span ids and option values are the same we use
            // the non-selected value to create our DOM object via getElementById
            hide_span = document.getElementById(sel.options[opts].value);
            hide_span.className = "hide";    // we reassign the DOM objects class
        }
    }
    // Now that we hid those that are not to be shown, we can show the span
    // that was selected.
    var show_span = document.getElementById(sel.options[idx].value);
    show_span.className = "show";
}
// -->
  </script>

</head>
<body>
  <div class="t">
    <div class="b">
      <div class="l">
        <div class="r">
          <div class="bl">
            <div class="br">
              <div class="tl">
                <div class="tr">
                  <div id="contact">
                    <h2>
                      Contacts</h2>
                    <ul id="contactnav" class="cf">
                      <li><a href="#1">1</a>
                        <div class="info" id="1">
                          <dl>
                            <dd class="img"><img src="img/icon.jpg" alt="" /></dd>
                            <dd><a href="">bla@bla.com</a></dd>
                            <dd>555-555-5555</dd>
                            <dd>info<a href="">map</a></dd>
                            <dd>info</dd>
                            <dd><a href="">chats</a> - <a href="">Emails</a></dd>
                          </dl>
                        </div>
                        <div class="toggleable">
                          <span class="show cf"><a href="">bla@bla.com</a></span>
                          <span class="hide">555-555-5555</span>
                        </div>
                      </li>
                      <li><a href="#2">2</a>
                        <div class="info" id="2">
                          <dl>
                            <dd class="img"><img src="img/icon.jpg" alt="" /></dd>
                            <dd><a href="">bla@bla.com</a></dd>
                            <dd>444-444-4444</dd>
                            <dd>info <a href="">map</a></dd>
                            <dd>info</dd>
                            <dd><a href="">chats</a> - <a href="">Emails</a></dd>
                          </dl>
                        </div>
                        <div class="toggleable">
                          <span class="show cf"><a href="">bla@bla.com</a></span>
                          <span class="hide">555-555-5555</span>
                        </div>
                      </li>
                      <li><a href="#3">3</a>
                        <div class="info" id="3">
                          <dl>
                            <dd class="img"><img src="img/icon.jpg" alt="" /></dd>
                            <dd><a href="">bla@bla.com</a></dd>
                            <dd>333-333-3333</dd>
                            <dd>info<a href="">map</a></dd>
                            <dd>info</dd>
                            <dd><a href="">chats</a> - <a href="">Emails</a></dd>
                          </dl>
                        </div>
                        <div class="toggleable">
                          <span class="show cf"><a href="">bla@bla.com</a></span>
                          <span class="hide">555-555-5555</span>
                        </div>
                      </li>
                    </ul>
                    <div id="toggle”">
                      <!-- in my IE7(Vista) w/o a form tag the options were displayed as text
As is the select was not there.
-->
                      <form action="" name="togglet">
                        <!-- Call the toggle function when the value of the select box is changed.
     Pass this (a reference to the select tag) as the only parameter
-->
                        <select onchange="toggle(this);" name="toggle">
                          <option value="email">Email address</option>
                          <option value="phone">Phone number</option>
                        </select>
                      </form>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>
</html>


I'm thinking I will need to create some sort of array for both the states possible? is that correct?

thanks again! biggrin.gif

This post has been edited by mhewson: 4 Jun, 2007 - 09:25 PM
User is offlineProfile CardPM
+Quote Post

Oaty
RE: Toggle Two Elements Via A Dropdown Menue
5 Jun, 2007 - 09:08 AM
Post #4

New D.I.C Head
*

Joined: 10 May, 2007
Posts: 28


My Contributions
I don't have the time right now to spell out an answer but think of these things.
(1) You dont want spaces in your class name "show cf", change it to "show_cf"
(2) In the toggle function, instead of using getElementById, use getElementsByTag('span') to get an array of your spans.
(3)loop through the array using getAttribute('class') to get the current class of the span. If the class does not match the value in the select input set it to hide, else set it to 'show_cf'.
(4)You've changed your class names, in your spans but not the values of your select options.
Your select should now read:
CODE

<select onchange="toggle(this);" name="toggle">
                          <option value="show_cf">Email address</option>
                          <option value="hide">Phone number</option>
                        </select>


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month