www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Rate Thread Display Modes
      #1  
    Old 02-05-2010, 11:28 AM
    JEMdesigns JEMdesigns is offline
    Registered User
     
    Join Date: Feb 2010
    Posts: 14
    resolved [RESOLVED] Need help with showing and hiding elements

    Hello,
    I am trying to using JavaScript to show and hide elements and a clients page. I have it set up that it with show the element by clicking a link and the same element hides by clicking another link that appears with the element. However what I also need now is if the element is visible and I click another link to display a new element, I want the first one to hide again.
    I know that it is an 'if' statement; however I don't know what the code is. Can anyone help?
    Thanks
    Reply With Quote
      #2  
    Old 02-05-2010, 11:37 AM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    You need to check and see what state the element is in and then do the opposite.

    Code:
    if (element == 'none') {
        //set element to display
    } else {
       // set element to not display set to none.
    }
    __________________
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman
    Reply With Quote
      #3  
    Old 02-05-2010, 12:13 PM
    JEMdesigns JEMdesigns is offline
    Registered User
     
    Join Date: Feb 2010
    Posts: 14
    That is what I thought hope when I put it in it doesn't seem to work.
    This is what I have for code:

    function showStuff(id) {
    if (element == 'none') {
    document.getElementById(id).style.display = 'block';
    } else {
    document.getElementById(id).style.display = 'none';
    }
    }
    function hideStuff(id) {
    document.getElementById(id).style.display = 'none';
    }

    ....
    <li><a href="#" onclick="showStuff('xp'); return false;">Introduction to Windows XP</a></li>
    <li><a href="#" onclick="showStuff('vista'); return false;">Introduction to Windows Vista</a></li>
    ....
    <span id="xp" style="display: none;">
    <p>Windows XP is an operating system that allows you to access other programs
    on the computer. This course is a beginner level course that teaches students
    how to navigate inside the operating system, manage files and general functions
    of the operating system.<a href="#" onclick="hideStuff('xp'); return false;">Close</a></p>
    </span>
    <span id="vista" style="display: none;">
    <p>Windows Vista was released between Windows XP and Windows 7 the latest operating system. A beginner level course that teaches students how to navigate inside the operating system, manage files and general functions of the operating system.<a href="#" onclick="hideStuff('vista'); return false;">Close</a></p>
    </span>
    Reply With Quote
      #4  
    Old 02-05-2010, 12:25 PM
    skywalker2208's Avatar
    skywalker2208 skywalker2208 is offline
    Registered User
     
    Join Date: Mar 2007
    Posts: 887
    I guess my explanation wasn't very good.


    You need to get the element style and check its value first.
    Code:
    function showStuff(id) {
    if (document.getElementById(id).style.display == 'none') {
    document.getElementById(id).style.display = 'block';
    } else {
    document.getElementById(id).style.display = 'none';
    }
    __________________
    "Hippies.They're everywhere. They wanna save the earth, but all they do is smoke pot and smell bad."-Cartman
    Reply With Quote
      #5  
    Old 02-05-2010, 01:34 PM
    JEMdesigns JEMdesigns is offline
    Registered User
     
    Join Date: Feb 2010
    Posts: 14
    Okay so that does it for that link, but what about if I click on another link, how do I make the old on vanish.
    So for example if I first click on Windows XP and it displays the info for it. So what I want to do is when I click on the Windows Vista, the Xp info hides and the Vista info displays.
    Reply With Quote
      #6  
    Old 02-05-2010, 05:06 PM
    elbaz elbaz is offline
    Registered User
     
    Join Date: Feb 2010
    Posts: 1
    A slightly different take...

    I would use the following two JavaScript functions to hide and show the elemets:

    function hideAllInfo() {
    var aSpans = document.getElementsByTagName("span");
    for (i = 0; i < aSpans.length; i++) {
    aSpans[i].style.display = 'none';
    }
    }
    function showInfo(id) {
    hideAllInfo();
    document.getElementById(id).style.display = 'block';
    return false;
    }
    Then in the HTML I would do something like:
    <div>
    <a onclick="return showInfo('Google');">What is Google</a>
    </div>
    <div>
    <a onclick="return showInfo('Yahoo');">What is Yahoo</a>
    </div>
    <span id="Google" style="display:none">
    <div>Here would be some info about Google.</div>
    <a href="http://www.google.com">Go to Google</a>
    </span>
    <span id="Yahoo" style="display:none">
    <div>Here would be some info about Yahoo.</div>
    <a href="http://www.yahoo.com">Go to Yahoo</a>
    </span>
    When you click on a link the first thing the showInfo function does is call the hideAllInfo function. The hideAllInfo function resets all the information areas back to non-display. showInfo then sets the selected areas display property so it will display. The nice thing about this approch is that you can add as many links and information areas as you want without having to change the JavaScript code.
    Reply With Quote
      #7  
    Old 02-08-2010, 03:40 AM
    omnicity omnicity is offline
    Registered User
     
    Join Date: Jan 2005
    Posts: 228
    One more little thing that you need to be aware of, is that the first time you try to read
    Code:
    style.display
    it will probably be empty, so you need to pay attention to your initial state.
    Reply With Quote
      #8  
    Old 02-08-2010, 02:34 PM
    JEMdesigns JEMdesigns is offline
    Registered User
     
    Join Date: Feb 2010
    Posts: 14
    Thanks elbaz! That was exactly what I was looking for.
    Reply With Quote
      #9  
    Old 02-08-2010, 04:00 PM
    rnd me's Avatar
    rnd me rnd me is offline
    working on the chain...
     
    Join Date: Jul 2008
    Location: urbana, il
    Posts: 1,793
    Quote:
    Originally Posted by omnicity View Post
    One more little thing that you need to be aware of, is that the first time you try to read
    Code:
    style.display
    it will probably be empty, so you need to pay attention to your initial state.
    if you use "" instead of "block", you can ignore the initial state, and not screw up thing like table cells, links, and others that look funky with display:block;

    Code:
    function showInfo(id) {
        hideAllInfo();
        return document.getElementById(id).style.display = '';
    }
    Reply With Quote
      #10  
    Old 02-09-2010, 06:50 AM
    wbport wbport is offline
    Registered User
     
    Join Date: Sep 2008
    Location: Jackson MS
    Posts: 94
    I saw this in a Sudoku solver. SetPage is called with the div to be displayed and showPage either displays or hides the divisions.
    Code:
    function showPage(sPage, bShow)
    {
    	var div = document.getElementById(sPage);
    	div.style.visibility = bShow ? 'visible' : 'hidden';
    	div.style.position = bShow ? 'static' : 'absolute';
    }
    function setPage(sPage)
    {
    	showPage('start', sPage == 'start');
    	showPage('details', sPage == 'details');
    	showPage('solver', sPage == 'solver');
    	showPage('about', sPage == 'about');
    }
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:05 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.