www.webdeveloper.com
+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2012
    Posts
    10

    Angry Switch Div Content Using Javascript

    Hello all--

    I've been searching around the Internet looking for a solution, and haven't been able to find one that works.

    Here is what I am dealing with... I have 4 text links, that when I click on each link, I want the unhidden div container to load. Although my syntax is incorrect, it's basically:

    //My 4 text links
    <a href="">link 1</a>
    <a href="">link 2</a>
    <a href="">link 3</a>
    <a href="">link 4</a>

    //The container I want all content divs to load into... and by default, to show the first container content
    <div id="container"></div>

    //The 4 container content divs.
    <div id="container1">Whole bunch of text 1</div>
    <div id="container2">Whole bunch of text 2</div>
    <div id="container3">Whole bunch of text 3</div>
    <div id="container4">Whole bunch of text 4</div>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    6,946

    Lightbulb

    Looks like you didn't search far enough...
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?266743-Switch-Div-Content-Using-Javascript&p=1229155#post1229155
    
    function showDiv(idInfo) {
      var sel = document.getElementById('divLinks').getElementsByTagName('div');
      for (var i=0; i<sel.length; i++) {
        sel[i].style.display = 'none';
      }
      document.getElementById('container'+idInfo).style.display = 'block';
    }
    </script>
    <style type="text/css">
    #container1, #container2, #container3, #container4 {
      display:none;
      border:3px solid blue;
      height:200px;
      overflow:hidden;
    }
    </style>
    
    </head>
    <body>
    <div id="linkDiv">
     <a href="#" onclick="showDiv('');return false">Home</a>
     <a href="#" onclick="showDiv('1');return false">link 1</a>
     <a href="#" onclick="showDiv('2');return false">link 2</a>
     <a href="#" onclick="showDiv('3');return false">link 3</a>
     <a href="#" onclick="showDiv('4');return false">link 4</a>
    </div>
    <p>
    
    <div id="container">
     The container I want all content divs to load into... and by default, to show the first container content
    </div>
    
    <!-- The 4 container content divs. -->
    <div id="divLinks">
     <div id="container1">Container #1<p>Whole bunch of text 1</div>
     <div id="container2">Container #2<p>Whole bunch of text 2</div>
     <div id="container3">Container #3<p>Whole bunch of text 3</div>
     <div id="container4">Container #4<p>Whole bunch of text 4</div>
    </div>
    
    </body>
    </html>
    BTW: You should enclose your code between [ code] and [ /code] tags (without the spaces)
    to make it easier for forum members to read, copy, test and/or debug.

    Good Luck!

  3. #3
    Join Date
    Oct 2012
    Posts
    10
    JMRKER, you are a real blessing! Thank you so much... after 6 hours of troubleshooting, you totally solved my problem. Now I have to stylize... I wish I was better at coding...

  4. #4
    Join Date
    Oct 2012
    Posts
    10
    JMRKER, how do I show default content? The page loads blank with just the text links?

  5. #5
    Join Date
    Dec 2005
    Location
    FL
    Posts
    6,946
    You will need to she where your code differs from mine as I am not much of a mind reader.

  6. #6
    Join Date
    Oct 2012
    Posts
    10
    For those of you using this code, place this code below <div id="container4"></div>

    Code:
        <div id="container">
    		<script language="javascript">
            $(document).ready(function(){
            showDiv('1');
            })           
            </script>
        </div>

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    6,946

    Exclamation

    Quote Originally Posted by maggiemkfung View Post
    For those of you using this code, place this code below <div id="container4"></div>

    Code:
        <div id="container">
    		<script language="javascript">
            $(document).ready(function(){
            showDiv('1');
            })           
            </script>
        </div>
    Should not be used unless you also include a JQuery library reference.

    Also, the code is not needed unless the user wants to display the first <div> tag after the main <div> content when the page is called the 1st time.
    There are other ways to accomplish this without the library [onload() comes to mind].
    And the description of the OP in the first post did not want that, if I understood his request.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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



Recent Articles