Solved

Show / Hide JQuery Script (Provided, working Example)

Posted on 2013-01-30
4
229 Views
Last Modified: 2013-02-02
Hello All;

I found this script from:
www.designing4u.de (Show/Hide)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</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(){  
  $('.ahref',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') {  
       $(".box"+item.id).html(html).slideDown(1000); 
        $(this).html("hide");
      } else { 
        $(this).html('hide');  
        $('.box'+id).slideDown(1000);  
      }  
    },  
    function () {  
      var id = $(this).attr('href');
      $(this).html('show');  
      $('.box'+id).slideUp(1000);  
    }  
  );
});
</script>
</head>

<body>
<a href="1" class="ahref">show 1</a>
<a href="2" class="ahref">show 2</a>
<a href="3" class="ahref">show 3</a>
<div class="box1">1</div> 
<div class="box2">2</div> 
<div class="box3">3</div>
<div style="clear:left"></div>
</body>
</html>

Open in new window


OK, I LOVE this code, and it WORKS in my project, so I want to use it so badly, I can taste it.
Now, my question is the following.

When you run the code, the following happened.

Step #1: You have to click the
show 1
show 2
show 3

Step #2: Then you have to click the
show
show
show

Step #3: Then the layers will hide themselves.
So, this is an unneeded step, that I would prefer not to have.

Can someone please let me know what needs to be done in order to eliminate this extra step.

Thank you
Carrzkiss
0
Comment
Question by:Wayne Barron
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
selvol earned 500 total points
ID: 38838700
Try this my Friend....

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</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(){  
  $('.ahref',this).toggle(
    function () {
      var id = $(this).attr('href');
      if($('.box'+id).text() == '') {  
       $(".box"+item.id).html(html).slideUp(1000); 
        $(this).html("hide");
      } else { 
        $(this).html('hide');  
        $('.box'+id).slideUp(1000);  
      }  
    },  
    function () {  
      var id = $(this).attr('href');
      $(this).html('show');  
      $('.box'+id).slideDown(1000);  
    }  
  );
});
</script>
</head>

<body>
<a href="1" class="ahref">show 1</a>
<a href="2" class="ahref">show 2</a>
<a href="3" class="ahref">show 3</a>
<div class="box1">1</div> 
<div class="box2">2</div> 
<div class="box3">3</div>
<div style="clear:left"></div>
</body>
</html>
                                  

Open in new window


I think I got the question.

Selvol
0
 
LVL 30

Author Closing Comment

by:Wayne Barron
ID: 38838717
instead of just coping and pasting, I looked through the code.
Took my 2 glances, to catch on to the SlideUp and SlideDown, being changed around.

I wonder why the developer missed that one?

However, you rock "Selvol"
Carrzkiss
0
 
LVL 17

Expert Comment

by:selvol
ID: 38838981
Don't be afraid to experiment with code.
That is just what I did.

I am glad to see you actually looked at the code and figured out what
was changed.


That in its self is really what I am here for (on EE).
I would rather answer questions knowing that the asker learns from it
and not just copies and pastes.......

Glad I could Help......

Selvol
0
 
LVL 30

Author Comment

by:Wayne Barron
ID: 38847029
You are not kidding there Selvol.

I helped this one person here a few years ago, and found out that all he did
Was copy and paste, and never paid one bit of attention to the code itself.
And then he started coming in and asking the same questions, over and over again.
It got sickening.
So I caught him on it, and made an example of him.
Then everyone that was helping him, started getting him to try different things
before they would assist him further.

I want to learn more, I want my knowledge to extend further than what it is.

Have a good weekend Selvol
Carrzkiss
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn how to dynamically set the form action using jQuery.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now