+ Reply to Thread
Results 1 to 4 of 4

Thread: JavaScript Show/Hide

  1. #1
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    JavaScript Show/Hide

    Hi guys. I am working on creating an online store type program for practice and I want to create a page where people can go in and edit the store by adding/removing items and changing the color schemes. I know this part of the program will be javascript heavy considering what i want it to do. Anyway I am stuck here on this part because I am pretty bad at javascript. What I want to do is make it so there are three tabs at the top of the divs and when they click on one of the tabs the corresponding div becomes visible, the other two divs become hidden and the tabs change to match (the clicked one gets a white background and loses the border-bottom). I was close with my javascript attempts but I am getting frustrated, please help!

    here is the code:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit</title>
    <style type="text/css">
        .Wrapper {position: relative; margin-right: auto; margin-left: auto; width: 500px;}
        .ChangesTable {width: 100%; margin: none; padding: none; border-collapse: collapse;}
        .AddItems {background: #FFF; border-bottom: none; border-top: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .RemoveItems {background: #D3D3D3; border-bottom: 1px #686868 solid; border-top: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .ColorScheme {background: #D3D3D3; border-bottom: 1px #686868 solid; border-top: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .DivAddItems {padding: 10px; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .DivRemoveItems {padding: 10px; display:none; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .DivColorScheme {padding: 10px; display:none; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
    </style>
    </head>
    <body>
    <div name="Wrapper" class="Wrapper">
        <table name="ChangesTable" class="ChangesTable">
            <tr>
                <td name="AddItems" class="AddItems">Add Items</td>
                <td name="RemoveItems" class="RemoveItems">Remove Items</td>
                <td name="ColorScheme" class="ColorScheme">Color Scheme</td>
            </tr>
        </table>
        <div name="DivAddItems" class="DivAddItems">
            <form method="post" action="AddItems.php">
                <label>Item Name</label><br /><input name="ItemName" type="text" /><br />
                <label>Price</label><br /><input name="ItemCost" type="text" /><br />
                <label>Brief Description</label><br /><textarea name="ItemDescription"></textarea><br />
            </form>
        </div>
        <div name="DivRemoveItems" class="DivRemoveItems">
        </div>
        <div name="DivColorScheme"  class="DivColorScheme">
        </div>
    </div>
    </body>
    </html>

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: JavaScript Show/Hide

    One: You could look into some free scripts and see how others do it

    http://www.barelyfitz.com/projects/tabber/

    Two: You could use a JavaScript toolkit like Dojo and use their widgets like I did:

    http://brightbeauty.x10hosting.com/t...container.html

    Three: Hack your code into working like I did:

    http://brightbeauty.x10hosting.com/abc.html

    Some points: you have to give your items id's. That is how you look them up. (document.getElementById) and manipulate their appearance by their 'style' attribute.

    The 'label' tag should have a for="someId" attribute that ties the label to the input field. That way, when you click on the label, the field will get focus. Your use of 'label' does nothing.


    My hack of your page, ask any questions about it:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Edit</title>
    <style type="text/css">
        .Wrapper {position: relative; margin-right: auto; margin-left: auto; width: 500px;}
        .ChangesTable {width: 100%; margin: none; padding: none; border-collapse: collapse;}
        
        .activeTab {   background: #FFF;    border-bottom: none;              border-top: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .inactiveTab {background: #D3D3D3; border-bottom: 1px #686868 solid; border-top: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
           
        .DivAddItems {padding: 10px; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .DivRemoveItems {padding: 10px; display:none; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
        .DivColorScheme {padding: 10px; display:none; left: -1px; border-collapse: collapse; position:relative; width: 479px; border-bottom: 1px #686868 solid; border-right: 1px #686868 solid; border-left: 1px #686868 solid;}
    </style>
    
    <script type="text/javascript">
    
    
    
       var current_tab = 'add' ;
       function addClicked(  ){
          if( current_tab == 'add' ){ return ; }
          document.getElementById('add').style.display = 'block' ;
          document.getElementById(current_tab).style.display = 'none' ;
          document.getElementById("AddItems").className = 'activeTab' ;
          document.getElementById("RemoveItems").className = 'inactiveTab' ;
          document.getElementById("ColorScheme").className = 'inactiveTab' ;
          current_tab = 'add' ;
    
       };
       function removeClicked(  ){
          if( current_tab == 'remove' ){ return ; }
          document.getElementById('remove').style.display = 'block' ;
          document.getElementById(current_tab).style.display = 'none' ;
          document.getElementById("AddItems").className = 'inactiveTab' ;
          document.getElementById("RemoveItems").className = 'activeTab' ;
          document.getElementById("ColorScheme").className = 'inactiveTab' ;
          current_tab = 'remove' ;
    
       };
       function colorClicked(  ){
          if( current_tab == 'color' ){ return ; }
          document.getElementById('color').style.display = 'block' ;
          document.getElementById(current_tab).style.display = 'none' ;
          document.getElementById("AddItems").className = 'inactiveTab' ;
          document.getElementById("RemoveItems").className = 'inactiveTab' ;
          document.getElementById("ColorScheme").className = 'activeTab' ;
          current_tab = 'color' ;
    
    
      };
    
       function  init(){
           document.getElementById('AddItems').onclick = addClicked ;
           document.getElementById('RemoveItems').onclick = removeClicked ;
           document.getElementById('ColorScheme').onclick = colorClicked ;
    
       };
    
       window.onload = init;  
    </script>
    </head>
    <body>
    <div name="Wrapper" class="Wrapper">
        <table name="ChangesTable" class="ChangesTable">
            <tr> 
                <td name="AddItems"    class="activeTab"    id='AddItems'    >Add Items</td>
                <td name="RemoveItems" class="inactiveTab" id="RemoveItems" >Remove Items</td>
                <td name="ColorScheme" class="inactiveTab" id="ColorScheme" >Color Scheme</td>
            </tr>
        </table>
        <div name="DivAddItems" class="DivAddItems" id="add" >
            <form method="post" action="AddItems.php">
                <label>Item Name</label><br /><input name="ItemName" type="text" /><br />
                <label>Price</label><br /><input name="ItemCost" type="text" /><br />
                <label>Brief Description</label><br /><textarea name="ItemDescription"></textarea><br />
            </form>
        </div>
        <div name="DivRemoveItems" class="DivRemoveItems" id="remove">
            <h1>REMOVE ITEMS FORM</h1>
        </div>
        <div name="DivColorScheme"  class="DivColorScheme" id="color">
           <h1>COLOR SCHEME FORM</h1>
        </div>
    </div>
    </body>
    </html>
    Nothing is always absolutely so.

  3. #3
    garrensilverwing's Avatar
    garrensilverwing is offline x10 Sophmore garrensilverwing is an unknown quantity at this point
    Join Date
    Nov 2008
    Posts
    148

    Re: JavaScript Show/Hide

    ah ok thanks very much, i had a similar thing going but my syntax was way off lol

  4. #4
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: JavaScript Show/Hide

    I would also suggest using jQuery. I'm no expert at it, not even at javascript, but I was able to make a couple div slide open and close on a click

    www.jquery.com
    http://www.learningjquery.com/2006/0...show-and-hide#
    Last edited by diabolo; 10-05-2009 at 07:39 PM.

+ Reply to Thread

Similar Threads

  1. Replies: 0
    Last Post: 08-17-2008, 01:46 AM
  2. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  3. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  4. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers