Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How can I repeat this sliding show / hide dynamic?

Posted on 2015-01-28
3
Medium Priority
?
112 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

926 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