Solved

How can I repeat this sliding show / hide dynamic?

Posted on 2015-01-28
3
105 Views
Last Modified: 2015-01-29
This works:

<!doctype html>
<html lang="en">
<head>  
<meta charset="utf-8">  
<title>jQuery UI Effects - Hide Demo</title>  
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
 $(document).ready(function(){
 
        $(".slidingDiv").hide();
        $(".show_hide").show();
 
    $('.show_hide').click(function(){
    $(".slidingDiv").slideToggle();
    });
 
});
 </script>
 
 <style>
.slidingDiv {
    height:300px;
    background-color: #99CCFF;
    padding:20px;
    margin-top:10px;
    border-bottom:5px solid #3399FF;
}
 
.show_hide {
    display:none;
}

</style>
 
 </head>
 
 <body>
 
 <a href="#" class="show_hide">Show/hide</a>
<div class="slidingDiv">
Fill this space with really interesting content. <a href="#" class="show_hide">hide</a></div>
 </body>
 
 </html>

Open in new window


My dilemma is that I want to repeat this show / hide dynamic in the context of a situation where I'll be looping through a database and I want that show / hide to appear with every question.

I started to think that what I could conceivably do is repeat that code for every question, but that seemed to me like I was overlooking something more elegant where I could simply pass in an "id" of some sort so the same code would effect different divs based on that id.

Sounds reasonable, but how?
0
Comment
Question by:brucegust
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
WebDevEM earned 500 total points
ID: 40576599
Hi,

It actually doesn't take very much to convert what you have into a FAQ-style list where each link toggles the DIV below it... Just take a look at jQuery's "this" and "next" features.  I've set up a Fiddle at http://jsfiddle.net/webdevem/8yo24y1b/ to show it in action, but here it is as well:

HTML
<div class="show_hide"><a href="#">Show/hide</a></div>
<div class="slidingDiv">
Fill this space with really interesting content. <a href="#" class="show_hide">hide</a></div>
<div class="show_hide"><a href="#">Show/hide</a></div>
<div class="slidingDiv">
Fill this space with really interesting content. <a href="#" class="show_hide">hide</a></div>
<div class="show_hide"><a href="#">Show/hide</a></div>
<div class="slidingDiv">
Fill this space with really interesting content. <a href="#" class="show_hide">hide</a></div>

Open in new window

JavaScript
$(".slidingDiv").hide();
$(".show_hide").show();

$('.show_hide').click(function(){
    $(this).next().slideToggle();
});

Open in new window


CSS
.slidingDiv {
    height:300px;
    background-color: #99CCFF;
    padding:20px;
    margin-top:10px;
    border-bottom:5px solid #3399FF;
}
 
.show_hide {
    display:none;
}

Open in new window


What that does is set up a click event that will target the NEXT div based on which one was clicked, rather than targeting one in particular.

Hope this helps!

WebDevEM
0
 
LVL 9

Expert Comment

by:WebDevEM
ID: 40576603
I've added a slight adjustment to the Fiddle so that the "Hide" link inside the content will hide its parent (the blue box containing it) on click.
$('.hideMe').click(function(){
    $(this).parent().slideUp();
});

Open in new window

0
 

Author Closing Comment

by:brucegust
ID: 40578121
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question