Solved

Show / Hide JQuery Script (Provided, working Example)

Posted on 2013-01-30
4
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

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 31

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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)

726 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