Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    44
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Show Hide Divide onchange of input

    This code works to show divide with id "it" when select option "it" is chosen and it shows divide "de" when option "de" is chosen.

    Problem: it will not show the nested divides inside the "it" and "de" divides. It only shows the <p></p> tags. Does someone know how I can get it to show the divides inside the main divide as well?

    Thanks in advance for your help.


    Head
    Code:
    function showstuff(divID)
    {
       els = document.getElementById( 'mydiv' ).getElementsByTagName( 'div' );
    
       for (var i = 0; i < els.length; i++)
       {
         els[i].style.visibility="hidden";
         els[i].style.display="none";
       }
    
       if( divID )
       {
         document.getElementById(divID).style.visibility="visible";
         document.getElementById(divID).style.display="block";
       }
    
       if( window.event )
         window.event.returnValue = false;
    
       return false;
    }
    window.onload=function()
    		{
    			showstuff('');
    		}
    Body
    Code:
    <select size="1" hide_label="0" id="experience" class=" validate['required']" title="" type="select" name="experience" onchange="showstuff(this.value) ;">
    <option value="">(Please select)</option>
    <option value="it">IT</option>
    <option value="de">Design Engineer</option>
    <option value="both">BOTH</option>
    </select>
    
    <div id="mydiv">
    
    <div id="it">
    <p>why dont you show anything else!</p>
    
    <div class="ccms_form_element cfdiv_header" id="_h3_it_disciplines_check_all_that_apply_h3___container_div"><h3>IT Disciplines (Check all that apply)</h3><div class="clear"></div></div>
    </div>
    <div id="de">
    
    <p>why dont you show anything else! UGH!!</p>
    <div class="ccms_form_element cfdiv_header" id="_h3_design_engineer_disciplines_check_all_that_apply_h3___container_div"><h3>Design Engineer Disciplines (Check all that apply)</h3><div class="clear"></div></div>
    <p>why dont you show anything else!</p>
    </div>
    </div>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    It's all in your code. If you examine it step by step, you'll notice that

    first you hide every single DIV (including the nested inner DIVs). Then you only show one specific DIV based on its id. But the nested inner DIVs will still be hidden.

    Solution: Only hide the outer DIVs in the first place. The nested inner DIVs will automatically be hidden together with the outer DIVs. Then if you show one of the outer DIVs, its inner DIVs will automatically show, too.

    Code:
    var outerDIVs = ["it", "de"]; // add more id's for outer DIVs here
    function showstuff(divID)
    {
       for (var i = 0; i < outerDIVs.length; i++)
       {
         document.getElementById(outerDIVs[i]).style.display="none";
       }
    
       if( divID )
       {
         document.getElementById(divID).style.display=""; // this will set display to default
       }
    
       if( window.event )
         window.event.returnValue = false;
    
       return false;
    }

  • Users who have thanked devnull69 for this post:

    Chrys (03-16-2012)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    44
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thank you so much for your reply. That makes a lot of sense.

    However, I tried implementing those changes and now I am getting an error:
    Uncaught ReferenceError: showstuff is not defined

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    44
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Got it now thank you!

    Is there any way that I can have it show all divides if a certain option is chosen?
    Last edited by Chrys; 03-14-2012 at 05:05 PM.

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Just put the following for loop into a function and call this function from the desired action (like onclick of a button etc.)
    Code:
       for (var i = 0; i < outerDIVs.length; i++)
       {
         document.getElementById(outerDIVs[i]).style.display="";
       }

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    44
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Just put the following for loop into a function and call this function from the desired action (like onclick of a button etc.)
    Code:
       for (var i = 0; i < outerDIVs.length; i++)
       {
         document.getElementById(outerDIVs[i]).style.display="";
       }
    I am confused by this... please forgive me if I am being silly.

    But I am calling the showstuff function onchange for the select. The function above would have to be called using a separate button not an option within the select I am already using. Right?

    I think I just need to add another line saying something like this:

    If divID = "both" show "de" and "it"

    I am just having trouble with the syntax of this.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Chrys View Post
    I think I just need to add another line saying something like this:

    If divID = "both" show "de" and "it"

    I am just having trouble with the syntax of this.
    I would do something like this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
            <title></title>
            <style type="text/css">
                #it,#de{
                    display: none;
                }
            </style>
        </head>
        <body>
            <select size="1" id="experience"  name="experience" onchange="showstuff(this) ;">
                <option value="">(Please select)</option>
                <option value="it">IT</option>
                <option value="de">Design Engineer</option>
                <option value="both">BOTH</option>
            </select>
            <div id="mydiv">
                <div id="it" class="showHideDivs">
                    <p>IT content</p>
                </div>
                <div id="de" class="showHideDivs">
                    <p>DE content</p>
                </div>
            </div>
            <script type="text/javascript">
                var showHideDivsO = document.getElementsByClassName('showHideDivs');
                function showstuff(selO){
                    showHideAll('none'); //first hide all the divs
                    if(selO.selectedIndex == 0){return;}
                    if(selO.value == 'both'){ //show all the divs
                        showHideAll('block');
                    } else { //show just the selected div
                        document.getElementById(selO.value).style.display='block';
                    }
                }
                function showHideAll(status){
                    if(status != 'none' && status != 'block'){status == 'block';}
                    for(i=0; i<showHideDivsO.length; i++){
                        showHideDivsO[i].style.display=status;
                    }
                }
            </script>
        </body>
    </html>
    Last edited by webdev1958; 03-16-2012 at 12:42 AM.

  • #8
    New Coder
    Join Date
    Aug 2011
    Posts
    44
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Cool

    That is a lot simpler than I was thinking. That makes a lot of sense. Thank you so much for your help with this!

  • #9
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    glad it's sorted


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •