www.webdeveloper.com
Results 1 to 3 of 3

Thread: Hide/show text

  1. #1
    Join Date
    Sep 2011
    Posts
    31

    Hide/show text

    Hi can someone find what's wrong with this code? It's a Q/A page, where all the answers are hidden at first, then an answer is displayed when a question is clicked (after hiding all other displayed answers). It's not working at all.

    JS:
    Code:
    <!--Q&A text-->
    	<script type="text/javascript">
    		
    		$(document).ready(function() {
    			$('.answer').hide();  
    			$('.question').click(function() {
    				var $nextDiv = $(this).next();
    				var $visibleSiblings = $nextDiv.siblings('div:visible');
    			 
    				if ($visibleSiblings.length ) {
    					$visibleSiblings.slideUp('fast', function() {
    					$nextDiv.slideToggle('fast');
    				});
    				} else {
    					$nextDiv.slideToggle('fast');
    				}
    			});
    		});
    
    	</script>
    HTML:
    Code:
    <div class="qatext">
    	<div class="question">Q: 1</div>
    	<div class="answer">A: 1</div>
            <div class="question">Q: 2</div>
    	<div class="answer">A: 2</div>
    </div>

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    You forgot to specify that only the ".answer" siblings should be hidden after the click. Now questions also gets hidden.
    Quote Originally Posted by Windbrand View Post
    Code:
    <!--Q&A text-->
        <script type="text/javascript">
            
            $(document).ready(function() {
                $('.answer').hide();  
                $('.question').click(function() {
                    var $nextDiv = $(this).next();
                    var $visibleSiblings = $nextDiv.siblings('div.answer:visible');
                 
                    if ($visibleSiblings.length ) {
                        $visibleSiblings.slideUp('fast', function() {
                        $nextDiv.slideToggle('fast');
                    });
                    } else {
                        $nextDiv.slideToggle('fast');
                    }
                });
            });
    
        </script>

  3. #3
    Join Date
    Sep 2011
    Posts
    31
    Thank you I will try that.

Thread Information

Users Browsing this Thread

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

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